首页 > 解决方案 > 处理多个请求时的 ASAuthorizationController 问题

问题描述

所以,问题来了:

如果我的钥匙串中有一个应用程序的电子邮件密码和苹果 ID 记录,并且想要请求该数据进行自动登录,我可以获得基于电子邮件密码的帐户信息。

Apple ID 无法按预期工作。我收到ASAuthorizationAppleIDCredential对象authorizationCode == nil。我确实收到credential.identityToken并可以解码令牌。这是一个有效的,但问题是我需要authorizationCode.

如果我使用 的独立login功能AppleAuthenticator,它可以正常工作。ASAuthorizationController完美执行performRequests,我可以authorizationCodeASAuthorizationAppleIDCredential. 问题在于AggregatedAuthenticator. ASAuthorizationController如果有多个请求传递给它,这似乎有点损坏并且无法获取所有数据。

AggregatedAuthenticator(anchor: view.window!).startAutoLogin() // does not work with apple id

AppleAuthenticator(anchor: view.window!).login() // works

唯一的区别是AppleAuthenticatorASAuthorizationController.

我能想到的唯一解决方法是,如果我去AggregatedAuthenticator那里再次登录,请参阅代码中的注释:

public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
    switch authorization.credential {
    case let appleIDCredential as ASAuthorizationAppleIDCredential:
        // Question: here we receive the credential without authorizationCode, but why?
        appleAuthenticator.login(with: appleIDCredential)
        // BUT if I do:
        // appleAuthenticator.login()
        // it works fine, but it shows that bottom sheet again asking for your face/touch-id again, but only for apple id this time
    case let emailPasswordPair as ASPasswordCredential:
        emailAuthenticator.login(with: emailPasswordPair)
    default:
        print("Irrelevant stuff")
    }
}

有什么想法吗,伙计们?

看看下面的示例代码:

https://github.com/SergeyPetrachkov/ASAuthControllerIssueSample

标签: iosswiftkeychainapple-sign-in

解决方案


好吧,伙计们,这是 iOS 15 的回归。以前一切都可以正常工作。


推荐阅读