Ios Cognito getIdentityId()在swift中不适用于未经身份验证的用户
我有以下代码,用于通过未经验证的用户在swift中获取IOS应用程序的Cognito ID:-Ios Cognito getIdentityId()在swift中不适用于未经身份验证的用户,ios,swift,amazon-cognito,Ios,Swift,Amazon Cognito,我有以下代码,用于通过未经验证的用户在swift中获取IOS应用程序的Cognito ID:- import Foundation import AWSCore import AWSCognito class CognitoInit { func getCognitoIdentityId() -> String { let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,
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()时,它总是返回“DEFAULT_COGNITO_ID”(我在模拟器中运行它)。通过调试器进入代码显示异步任务没有在内部运行(从if(task.error!=nil){返回nil的整个代码块被绕过)
我是否错过了一些我能想到的东西
请提供帮助。我是swift开发的新手。听起来可能只是调用的异步性质。您能否完全按照中所述尝试该示例,等待几秒钟,然后尝试通过获取标识id
credentialsProvider.identityId
看看会发生什么?因为它是异步的,所以我认为以这种方式设置它不会像你希望的那样工作。你需要事先启动异步调用。谢谢,我在Android中也做了类似的事情,它会立即返回。关于获取身份ID的等待时间有什么建议吗?Android不是异步的s、 它可能只是缓存的。时间会因设备而异,这只是一个猜测,但你可以很好地确定它的时间。不,我正在显式地努力将其放入aysnc任务中。啊,那么是的,它是异步的。这是两者之间不幸的脱节。