swift - 在恢复 session 期间使用 AWS Cognito 确定登录类型 (Swift)

标签 swift amazon-cognito

我很难弄清楚如何在使用 AWS Cognito 的恢复 session 期间确定登录类型。我的代码基于 MobileHub 示例(如下)。

我集成了用户池的名称/密码模式(帐户创建和登录)以及 Facebook 登录按钮,一切都运行良好。

我的应用程序中有一些逻辑需要根据登录类型采取不同的行为,但我不知道如何做到这一点。

有人做过吗?

    func didFinishLaunching(_ application: UIApplication, withOptions launchOptions: [AnyHashable: Any]?) -> Bool {
    print("didFinishLaunching:")

    // Register the sign in provider instances with their unique identifier
    AWSSignInManager.sharedInstance().register(signInProvider: AWSFacebookSignInProvider.sharedInstance())
    AWSIdentityProfileManager.sharedInstance().register(FacebookIdentityProfile.sharedInstance(), forProviderKey: AWSFacebookSignInProvider.sharedInstance().identityProviderName)
    AWSSignInManager.sharedInstance().register(signInProvider: AWSCognitoUserPoolsSignInProvider.sharedInstance())
    AWSIdentityProfileManager.sharedInstance().register(UserPoolsIdentityProfile.sharedInstance(), forProviderKey: AWSCognitoUserPoolsSignInProvider.sharedInstance().identityProviderName)

    setupAPIGateway()
    setupS3()

    let didFinishLaunching: Bool = AWSSignInManager.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)

    if (!isInitialized) {
        AWSSignInManager.sharedInstance().resumeSession(completionHandler: { (result: Any?, authState: AWSIdentityManagerAuthState, error: Error?) in
            print("didFinishLaunching Result: \(String(describing: result)) AuthState: \(authState) \n Error:\(String(describing: error))")
            if authState == .authenticated {
                // Facebook or Cognito???
                AWSCognitoUserAuthHelper.getCurrentUserAttribute(name: "sub", completionHandler: { (userid) in
                    // we need to fetch the user
                    ObjectManager.instance.getUser(userid: userid, completionHandler: { (user) in
                        ObjectManager.instance.setCurrentUser(user: user)
                    })
                })
            }
        }) // If you get an EXC_BAD_ACCESS here in iOS Simulator, then do Simulator -> "Reset Content and Settings..."
        // This will clear bad auth tokens stored by other apps with the same bundle ID.
        isInitialized = true
    }

    return didFinishLaunching
}

最佳答案

我发现的一个解决方案是转换为不同的身份配置文件类型,如下所示:

let identityManager = AWSIdentityManager.default()

if let fbIdentityProfile = identityManager.identityProfile as? FacebookIdentityProfile {
    print("didFinishLaunching - Facebook login")
} else if let upIdentityProfile = identityManager.identityProfile as? UserPoolsIdentityProfile {
    print("didFinishLaunching - User Pools login")
}

我可以围绕这个在我的应用程序中建立逻辑模型。不确定是否有更简洁的方法使用 MobileHub 帮助程序类或 AWS API,但这可行。

关于swift - 在恢复 session 期间使用 AWS Cognito 确定登录类型 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45656633/

相关文章:

ios - AWS Cognito 检查和恢复 session (iOS)

ios - 如何在 View 出现时以编程方式点击状态栏?

swift - 禁用时自定义按钮不显示为灰色

ios - 自动验证 Amazon Cognito 用户池中的用户

amazon-web-services - 自定义 UI 的 Cognito 授权代码授予流程

ios - iOS AWS S3 Upload如何重试失败或失败的任务?

ios - 无法使用参数传递 Timer.sheduledTimer

swift - 如何从 Haneke 的异步获取方法返回值

ios - bundle (标识符 : "org.cocoapods.MyPrivatePod") return nil

javascript - AWS Cognito 正确的用户流程