Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
SDK for Swift를 사용한 Amazon SNS 예제
다음 코드 예제에서는 Amazon SNS에서 AWS SDK for Swift를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 관련 시나리오의 컨텍스트에 따라 표시되며, 개별 서비스 함수를 직접적으로 호출하는 방법을 보여줍니다.
각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.
시작
다음 코드 예시는 Amazon SNS 사용을 시작하는 방법을 보여줍니다.
- SDK for Swift
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. Package.swift 파일입니다.
import PackageDescription let package = Package( name: "sns-basics", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13), .iOS(.v15) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"), .package( url: "https://github.com/apple/swift-argument-parser.git", branch: "main" ) ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "sns-basics", dependencies: [ .product(name: "AWSSNS", package: "aws-sdk-swift"), .product(name: "ArgumentParser", package: "swift-argument-parser") ], path: "Sources") ] )
기본 Swift 프로그램입니다.
import ArgumentParser import AWSClientRuntime import AWSSNS import Foundation struct ExampleCommand: ParsableCommand { @Option(help: "Name of the Amazon Region to use (default: us-east-1)") var region = "us-east-1" static var configuration = CommandConfiguration( commandName: "sns-basics", abstract: """ This example shows how to list all of your available Amazon SNS topics. """, discussion: """ """ ) /// Called by ``main()`` to run the bulk of the example. func runAsync() async throws { let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) var topics: [String] = [] let outputPages = snsClient.listTopicsPaginated( input: ListTopicsInput() ) // Each time a page of results arrives, process its contents. for try await output in outputPages { guard let topicList = output.topics else { print("Unable to get a page of Amazon SNS topics.") return } // Iterate over the topics listed on this page, adding their ARNs // to the `topics` array. for topic in topicList { guard let arn = topic.topicArn else { print("Topic has no ARN.") return } topics.append(arn) } } print("You have \(topics.count) topics:") for topic in topics { print(" \(topic)") } } } /// The program's asynchronous entry point. @main struct Main { static func main() async { let args = Array(CommandLine.arguments.dropFirst()) do { let command = try ExampleCommand.parse(args) try await command.runAsync() } catch { ExampleCommand.exit(withError: error) } } }
-
API 세부 정보는 AWS SDK for Swift API 참조의 ListTopics
를 참조하세요.
-
주제
작업
다음 코드 예시에서는 CreateTopic
을 사용하는 방법을 보여 줍니다.
- SDK for Swift
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) let output = try await snsClient.createTopic( input: CreateTopicInput(name: name) ) guard let arn = output.topicArn else { print("No topic ARN returned by Amazon SNS.") return }
-
API 세부 정보는 AWS SDK for Swift API 참조의 CreateTopic
을 참조하세요.
-
다음 코드 예시에서는 DeleteTopic
을 사용하는 방법을 보여 줍니다.
- SDK for Swift
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) _ = try await snsClient.deleteTopic( input: DeleteTopicInput(topicArn: arn) )
-
API 세부 정보는 AWS SDK for Swift API 참조의 DeleteTopic
을 참조하세요.
-
다음 코드 예시에서는 ListTopics
을 사용하는 방법을 보여 줍니다.
- SDK for Swift
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) var topics: [String] = [] let outputPages = snsClient.listTopicsPaginated( input: ListTopicsInput() ) // Each time a page of results arrives, process its contents. for try await output in outputPages { guard let topicList = output.topics else { print("Unable to get a page of Amazon SNS topics.") return } // Iterate over the topics listed on this page, adding their ARNs // to the `topics` array. for topic in topicList { guard let arn = topic.topicArn else { print("Topic has no ARN.") return } topics.append(arn) } }
-
API 세부 정보는 AWS SDK for Swift API 참조의 ListTopics
를 참조하세요.
-
다음 코드 예시에서는 Publish
을 사용하는 방법을 보여 줍니다.
- SDK for Swift
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) let output = try await snsClient.publish( input: PublishInput( message: message, topicArn: arn ) ) guard let messageId = output.messageId else { print("No message ID received from Amazon SNS.") return } print("Published message with ID \(messageId)")
-
API 세부 정보는 AWS SDK for Swift API 참조의 게시
를 참조하세요.
-
다음 코드 예시에서는 Subscribe
을 사용하는 방법을 보여 줍니다.
- SDK for Swift
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. 이메일 주소로 주제 구독.
let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) let output = try await snsClient.subscribe( input: SubscribeInput( endpoint: email, protocol: "email", returnSubscriptionArn: true, topicArn: arn ) ) guard let subscriptionArn = output.subscriptionArn else { print("No subscription ARN received from Amazon SNS.") return } print("Subscription \(subscriptionArn) created.")
SMS로 알림을 받으려면 주제의 전화번호를 구독합니다.
let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) let output = try await snsClient.subscribe( input: SubscribeInput( endpoint: phone, protocol: "sms", returnSubscriptionArn: true, topicArn: arn ) ) guard let subscriptionArn = output.subscriptionArn else { print("No subscription ARN received from Amazon SNS.") return } print("Subscription \(subscriptionArn) created.")
-
API 세부 정보는 AWS SDK for Swift API 참조의 구독
을 참조하세요.
-
다음 코드 예시에서는 Unsubscribe
을 사용하는 방법을 보여 줍니다.
- SDK for Swift
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) _ = try await snsClient.unsubscribe( input: UnsubscribeInput( subscriptionArn: arn ) ) print("Unsubscribed.")
-
API 세부 정보는 AWS SDK for Swift API 참조의 구독 취소
를 참조하세요.
-