4

这是我的场景。

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. 首先,我应该实现保存 userIdentifieriCloud Keychain吗?那我需要添加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
            }
        }

}

这并不符合我的意图。

有没有好心的老师可以回答我的问题?
谢谢你。

4

1 回答 1

6

你不需要做任何事情。Apple 通过将您的应用程序包与用户 iCloud 中的身份相关联来为您处理这一切。

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

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

当此代码运行时,如果发现现有帐户关联,则会提示他们进行身份验证。如果没有找到帐户,则不会发生任何事情。

于 2020-01-23T03:21:53.203 回答