ios - 在 iOS 应用程序中使用 AWS Cognito 和 Facebook 登录时 IdentityId 为 Nil

标签 ios swift amazon-web-services aws-sdk amazon-cognito

我有一个应用程序通过 AWS Cognito 使用 Facebook 提供商成功登录。登录后直接使用该应用程序可以检索身份,一切似乎都运行良好。

但是,经过一段时间(大约 > 1 小时)后,当返回应用程序时,身份 ID 似乎并未填写。奇怪的是用户名仍然被填写 - 只是没有 id。

    let identity = self.getLoggedInIdentity()
    let id = identity?.identityId      //this is nil
    let username = identity?.userName  //this still has a value

    func getLoggedInIdentity() -> AWSIdentityManager? {
        if userIsLoggedIn() {
            return AWSIdentityManager.defaultIdentityManager()
        }

        return nil
    }

    func userIsLoggedIn() -> Bool {
        let identityManager = AWSIdentityManager.defaultIdentityManager()
        return identityManager.loggedIn
    }

我从 AWS 示例应用程序开始,包括由 AWS Mobile Hub 自动生成的 AWSMobileHubHelper 框架。

我认为问题在于 session 未成功恢复,因为重新启动应用程序后此函数内的完成 block 永远不会触发:

func didFinishLaunching(application: UIApplication, withOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    print("didFinishLaunching:")

    if (!isInitialized) {
        print("AWSIdentityManager Attempt Resume Session")
        AWSIdentityManager.defaultIdentityManager().resumeSessionWithCompletionHandler({(result: AnyObject?, error: NSError?) -> Void in
                print("AWSIdentityManager Resume Session Result: \(result) \n Error:\(error)")
        })
        isInitialized = true
    }
    let didFinishLaunching: Bool = AWSIdentityManager.defaultIdentityManager().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)

    return didFinishLaunching
}

我已经验证了在重新启动应用程序时正在调用该函数,并且正在调用resumeSessionWithCompletionHandler,但完成处理程序从未被触发,我假设 session 没有成功恢复。

这里还有初始登录功能供引用:

@IBAction func handleFacebookLogin(sender: AnyObject) {
    self.handleLoginWithSignInProvider(AWSFacebookSignInProvider.sharedInstance())
}

func handleLoginWithSignInProvider(signInProvider: AWSSignInProvider) {
    AWSIdentityManager.defaultIdentityManager().loginWithSignInProvider(signInProvider, completionHandler: {(result: AnyObject?, error: NSError?) -> Void in
        // If no error reported by SignInProvider, discard the sign-in view controller.
        if error == nil {
            dispatch_async(dispatch_get_main_queue(),{
                self.dismissViewControllerAnimated(true, completion: nil)
            })
        }
        print("result = \(result), error = \(error)")
    })
}

有什么想法为什么我能够最初登录并连接到其他 AWS 服务,但在一段时间过后无法恢复 session ?

最佳答案

我也遇到了同样的问题。 我在 AppDelegate 上这样修复了。 你实现了这段代码吗?

    func application(application: UIApplication,openURL url: NSURL,sourceApplication: String?,annotation: AnyObject?) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

关于ios - 在 iOS 应用程序中使用 AWS Cognito 和 Facebook 登录时 IdentityId 为 Nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337967/

相关文章:

ios - viewDidAppear 出现在代码中会扰乱布局

ios - 从 UItabbarcontroller 中移除 morebutton TabbarItem

ios - XCTest无法读取启动参数

android - AWS Android SDK 连接超时和重试

hadoop - 亚马逊 EMR 和 Hive : Getting a "java.io.IOException: Not a file" exception when loading subdirectories to an external table

amazon-web-services - AWS Certificate Manager - 如何为内部 ALB 创建公共(public)证书?

ios - Swift:tableView.reloadData() 方式太慢

c++ - 具有已定义宏的 iOS 预处理条件导致 "token is not valid"

ios - 如何使用带有数组 (Dictionary<String, [String]>) 的快速字典中的定义来显示在表格 View 单元格上?

ios - 如何从 Swift 引用 CMAttitudeReferenceFrame.CMAttitudeReferenceFrameXArbitraryCorrectedZVertical