如何找出具有相同Apple ID的多个设备的用户的"Sign in with 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中实现保存用户标识符吗?那么我是否需要添加Keychain group Capability
  2. 首先,我真的不知道把performExistingAccountSetupFlows功能放在哪里。我想在用户触摸按钮时呈现授权控制器。所以我尝试了这种方法。
@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 通过将你的 app bundle 与用户 iCloud 中的身份相关联来为你处理这一切。

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

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

运行此代码时,如果找到现有帐户关联,系统将提示他们进行身份验证。 如果未找到帐户,则不会发生任何操作。