如何在具有相同Apple ID的多个设备的用户中找出“使用Apple登录”?

这是我的情况。

Someone has iPhone and iPad (iOS 13.0 or later),and he signed up with same appleID.
And he `sign in with apple` in his iPhone and try to `sign in with apple` again in his iPad.

我不希望他通过触摸登录按钮来编辑他的名字或两次检查共享或隐藏他的电子邮件两次来“用苹果登录”。

我从示例代码中发现了一些可以帮助我解决上述情况的代码

func performExistingaccountSetupFlows() {
    // Prepare requests for both Apple ID and password providers.
    let requests = [ASAuthorizationAppleIDProvider().createRequest(),ASAuthorizationPasswordProvider().createRequest()]

    // Create an authorization controller with the given requests.
    let authorizationController = ASAuthorizationController(authorizationRequests: requests)
    authorizationController.delegate = self
    authorizationController.presentationContextProvider = self
    authorizationController.performRequests()
}

我发现ASPasswordCredential将帮助我定义凭据用户

  1. 首先,我应该在iCloud Keychain中实现保存userIdentifier吗?那我需要添加Keychain group Capability吗?
  2. 首先,我真的不知道将performExistingaccountSetupFlows函数放在哪里。我想在用户触摸按钮时显示AuthorizationController。所以我尝试了这种方法。
@available(iOS 13.0,*)
@objc private func handleAuthorizationAppleIDButtonPress() {
    let appleIDProvider = ASAuthorizationAppleIDProvider()
    appleIDProvider.getcredentialState(
        forUserID: KeychainItem.currentUserIdentifier ?? "") { [weak self] (credentialState,error) in
        guard let `self` = self else { return }
        log.debugPrint(KeychainItem.currentUserIdentifier ?? "nil")

        switch credentialState {
            case .authorized:
                // The Apple ID credential is valid. Show Home UI Here

                // MARK: Existing iCloud keychain 
                self.performExistingaccountSetupFlows() 
                break

            case .revoked,.notFound:
                let request = appleIDProvider.createRequest()
                request.requestedScopes = [
                    .fullName,.email
                ]

                let controller = ASAuthorizationController(authorizationRequests: [request])
                controller.delegate = self
                controller.presentationContextProvider = self
                controller.performRequests()
                break
            default:
                break
            }
        }

}

这不符合我的预期。

有什么好心的老师可以回答我的问题吗?
谢谢。

kjhsdgfhkjshfuwerytu 回答:如何在具有相同Apple ID的多个设备的用户中找出“使用Apple登录”?

您无需执行任何操作。 Apple通过将您的应用程序捆绑包与用户iCloud中的身份相关联来为您处理所有这一切。

如果他们在具有相同iCloud帐户的另一台设备上运行您的应用程序,则当他们使用“使用Apple登录”时,他们将没有机会创建新帐户-只会提示他们使用现有帐户登录帐户。

您可以选择使用performExistingAccountSetupFlows()中的代码来提示用户登录,而无需点击“使用Apple登录”按钮;

运行此代码时,如果找到现有的帐户关联,则将提示他们进行身份验证。如果没有找到帐户,则什么也不会发生。

本文链接:https://www.f2er.com/2727997.html

大家都在问