我最近在一个使用 Amazon SNS 和 AWS SDK 2.1.1 的工作应用程序中将 AWS iOS SDK 升级到 2.2.3。我遇到了很多关于 BFTask 的编译错误。我发现我应该将代码中的所有 BFTask 名称更改为 AWSTask。然后一切都编译好了,我就可以运行应用程序了。但现在我在运行时遇到问题。当我创建 SNS 端点时,返回的 AWSTask 不显示错误,也不显示异常。它是成功的,结果非零。但是结果中的endpointARN是nil!我已经从控制台创建了我的 SNS 应用程序。
这是我正在使用的政策的相关片段:
{
"Effect": "Allow",
"Action": [
"SNS:CreatePlatformEndpoint"
],
"Resource": [
"My application ARN"
]
},
{
"Effect": "Allow",
"Action": [
"SNS:Subscribe"
],
"Resource": [
"My topic"
]
}
而且,这是我的 Swift 代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Some code...
// ...
UIApplication.sharedApplication().registerForRemoteNotifications()
// Some code...
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// remove the spaces from the device token
var token = deviceTokenAsString(deviceToken)
var platformEndpointRequest = AWSSNSCreatePlatformEndpointInput()
platformEndpointRequest.token = token as String
platformEndpointRequest.platformApplicationArn = SNS_APP_ARN
let snsManager = AWSSNS.defaultSNS()
snsManager.createPlatformEndpoint(platformEndpointRequest).continueWithBlock { (task: AWSTask!) -> AnyObject! in
if task.error != nil {
NSLog("createPlatformEndpoint failed: \(task.error)")
} else if task.exception != nil {
NSLog("createPlatformEndpoint failed: \(task.exception)")
} else if task.result != nil {
NSLog("task.result: \(task.result)")
let createEndpointResponse = task.result as! AWSSNSCreateEndpointResponse
NSLog("createEndpointResponse: \(createEndpointResponse)")
var endpointARN = createEndpointResponse.endpointArn
NSLog("createPlatformEndpoint succeeded, endpoint ARN: \(endpointARN)")
// Some more code...
}
return nil
}.continueWithBlock { (task: AWSTask!) -> AnyObject! in
if task.error != nil{
NSLog("sunscribe failed: \(task.error)")
} else if task.exception != nil {
NSLog("sunscribe failed: \(task.exception)")
} else if task.result != nil {
NSLog("sunscribe succeeded")
}
return nil
}
}
非常感谢您的帮助。谢谢!
最佳答案
这里我用 swift 3 更新了 sdk 2.4.1 版本 为端点订阅主题
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .usEast1, identityPoolId: SNSItentityBoolId)
let configuration = AWSServiceConfiguration(region: .usEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let sns = AWSSNS.default()
let request = AWSSNSCreatePlatformEndpointInput()
request?.token = self.strDeviceToken
//Send Request
request?.platformApplicationArn = SNSPlatformApplicationArn
sns.createPlatformEndpoint(request!).continue({ (task: AWSTask!) -> AnyObject! in
print("blah")
if task.error != nil {
print("Error: \(task.error)")
} else {
let createEndpointResponse = task.result! as AWSSNSCreateEndpointResponse
print("endpointArn: \(createEndpointResponse.endpointArn)")
let subscription = "*****" //Use your own topic endpoint
let subscriptionRequest = AWSSNSSubscribeInput()
subscriptionRequest?.protocols = "application"
subscriptionRequest?.topicArn = subscription
subscriptionRequest?.endpoint = createEndpointResponse.endpointArn
sns.subscribe(subscriptionRequest!).continue ({
(task:AWSTask) -> AnyObject! in
if task.error != nil
{
print("Error subscribing: \(task.error)")
return nil
}
print("Subscribed succesfully")
print("Success Message \(task.result)")
// let subscriptionConfirmInput = AWSSNSConfirmSubscriptionInput()
// subscriptionConfirmInput?.token = createEndpointResponse.endpointArn
// subscriptionConfirmInput?.topicArn = subscription
// sns.confirmSubscription(subscriptionConfirmInput!).continue ({
// (task:AWSTask) -> AnyObject! in
// if task.error != nil
// {
// print("Error subscribing: \(task.error)")
// }
// return nil
// })
return nil
})
}
return nil
})
关于ios - 升级到 AWS iOS SDK 到 2.2.3 后,AWS SNS createPlatformEndpoint 返回 nil endpointArn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31887288/