查看了类似的 SO 问题,但没有帮助我解决问题。
我在我们的 iOS 应用程序中使用 AWS Cognito 用户池。我们能够成功创建并登录用户。但是大约一个小时后,访问 token 不可用,我从 AWS Cognito 文档中了解到,iOS SDK 会自动刷新 (also mentioned here) 并在 token 不可用时获取 token ,但我不这样做看不到这种行为。下面的代码显示了我如何尝试获取访问 token 。
使用 iOS 开发工具包 AWSCognitoIdentityProvider 2.6.7
请告诉我如何解决这个问题。
let mySession = self.pool.currentUser()?.getSession()
guard let accessToken = mySession?.result?.accessToken?.tokenString as? String else {
print("Unable to obtain access token")
self.handleSignOut() // Signing out the user since there is no access token
return
}
最佳答案
getSession()
返回一个 AWSTask。
您必须在回调中访问 tokenString
。
以下代码适用于我:
self.pool.currentUser()?.getSession().continueWith { task in
if task.error == nil {
let session = task.result! as AWSCognitoIdentityUserSession
guard let accessToken = session.idToken?.tokenString; as? String else {
print("Unable to obtain access token")
self.handleSignOut() // Signing out the user since there is no access token
}
}
}
关于swift - iOS Swift、AWS Cognito 用户池、无法刷新访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49142054/