IdentityId在iOS App中使用AWS Cognito和Facebook登录为零

时间:2016-03-31 15:43:52

标签: 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框架。

我认为问题是会话没有成功恢复,因为重启应用程序后,此函数内的完成块永远不会触发:

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但是从不触发完成处理程序,并且我假设会话未成功恢复。

此处参考的是初始登录功能:

@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服务,但是在一段时间过后无法恢复会话的任何想法?

1 个答案:

答案 0 :(得分:0)

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

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