swift - iOS Swift、AWS Cognito 用户池、无法刷新访问 token

标签 swift swift3 amazon-cognito

查看了类似的 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/

相关文章:

swift - 尝试打开 Realm 数据库时出现错误 "unsupported version"

swift - Eureka swift : Dismiss Controller for MultipleSelectorRow

iOS 图表 - 分组条形图未正确索引

swift - 模棱两可地使用 observe firebase DB

amazon-cognito - 如何将 bool OR 添加到 cognito-idp list-user 的 --filter 选项

ios - Swift - 使用 tableView.dequeueReusableCell 时数据无法正确显示

swift 3 : How do I enable flash on custom AVFoundation camera?

ios - 如何让 SpriteKit Playgrounds 在 Xcode 8 beta 中工作?

ios - 适用于 iOS 的 AWS Cognito 是否允许离线登录?

python - 带有 AWS cognito 的无服务器框架生成 CORS 错误