对于未经身份验证的用户,Cognito getIdentityId() 在 swift 中不起作用
Cognito getIdentityId() does not work in swift for unauthenticated user
我有以下代码用于通过 swift 中未经身份验证的用户获取 IOS 应用程序的 Cognito ID:-
import Foundation
import AWSCore
import AWSCognito
class CognitoInit {
func getCognitoIdentityId() -> String {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,
identityPoolId:"My_idenitity_pool_id_in_aws")
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
credentialsProvider.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in
if (task.error != nil) {
return task.error?.localizedDescription
}
else {
// the task result will contain the identity id
return task.result
}
return nil
}
return "DEFAULT_COGNITO_ID"
}
}
当存根 ViewController 中的 getCognitoIdentityId() 被调用时,它总是 returns "DEFAULT_COGNITO_ID"(我 运行 在模拟器中调用它)。通过调试器进入代码显示异步任务没有 运行 内部(从 if (task.error != nil) { 到 return nil 的整个代码块被绕过。
我是不是漏掉了什么。我能想到的东西
- network/async 调用是否需要单独启用的权限?
- Cognito Identity Pool 中是否需要我必须执行的配置。
- 异步任务是否在模拟器中被阻止?
请帮忙。我是 swift 开发的新手。
听起来这可能只是该调用的异步性质。您能否完全按照 documentation 中的描述尝试示例,等待几秒钟,然后尝试通过
获取身份 ID
credentialsProvider.identityId
看看会发生什么?因为它是异步的,所以以这种方式设置它不会像您希望的那样工作,我认为。您需要事先启动异步调用。
我有以下代码用于通过 swift 中未经身份验证的用户获取 IOS 应用程序的 Cognito ID:-
import Foundation
import AWSCore
import AWSCognito
class CognitoInit {
func getCognitoIdentityId() -> String {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,
identityPoolId:"My_idenitity_pool_id_in_aws")
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
credentialsProvider.getIdentityId().continueWithBlock { (task: AWSTask!) -> AnyObject! in
if (task.error != nil) {
return task.error?.localizedDescription
}
else {
// the task result will contain the identity id
return task.result
}
return nil
}
return "DEFAULT_COGNITO_ID"
}
}
当存根 ViewController 中的 getCognitoIdentityId() 被调用时,它总是 returns "DEFAULT_COGNITO_ID"(我 运行 在模拟器中调用它)。通过调试器进入代码显示异步任务没有 运行 内部(从 if (task.error != nil) { 到 return nil 的整个代码块被绕过。
我是不是漏掉了什么。我能想到的东西
- network/async 调用是否需要单独启用的权限?
- Cognito Identity Pool 中是否需要我必须执行的配置。
- 异步任务是否在模拟器中被阻止?
请帮忙。我是 swift 开发的新手。
听起来这可能只是该调用的异步性质。您能否完全按照 documentation 中的描述尝试示例,等待几秒钟,然后尝试通过
获取身份 IDcredentialsProvider.identityId
看看会发生什么?因为它是异步的,所以以这种方式设置它不会像您希望的那样工作,我认为。您需要事先启动异步调用。