如何计算 "Sign in with Apple" 在具有相同 apple id 的多个设备的用户中?

How to figure out "Sign in with Apple" in user with multiple devices with same apple id?

这是我的场景。

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.

我不希望他在点击登录按钮时 "sign in with apple" 两次编辑他的名字或选中共享或隐藏他的电子邮件。

我从示例代码中找到了一些可以帮助我上述场景的代码

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
            }
        }

}

这并没有达到我的预期。

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

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

如果他们 运行 您的应用程序在另一台具有相同 iCloud 帐户的设备上,那么他们在使用 "Sign in with Apple" 时没有机会创建新帐户 - 他们只会收到提示使用现有帐户登录。

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

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