쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

SDK for Swift를 사용한 Amazon SNS 예제 - AWS SDK 코드 예제

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

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를 참조하세요.

다음 코드 예시는 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을 참조하세요.

다음 코드 예시에서는 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을 참조하세요.

다음 코드 예시에서는 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를 참조하세요.

다음 코드 예시에서는 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 참조게시를 참조하세요.

다음 코드 예시에서는 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 참조구독을 참조하세요.

다음 코드 예시에서는 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 참조구독 취소를 참조하세요.

다음 코드 예시에서는 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 참조구독 취소를 참조하세요.

프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.