From 482756f6f0125a410e57fb23dc69cf5e028b0fc4 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Thu, 30 Oct 2025 14:35:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96apple=20sign?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- keyBoard/Class/Manager/AppleSignInManager.h | 3 +++ keyBoard/Class/Manager/AppleSignInManager.m | 22 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/keyBoard/Class/Manager/AppleSignInManager.h b/keyBoard/Class/Manager/AppleSignInManager.h index 5c2b73d..05dc7c6 100644 --- a/keyBoard/Class/Manager/AppleSignInManager.h +++ b/keyBoard/Class/Manager/AppleSignInManager.h @@ -26,6 +26,9 @@ typedef void (^KBAppleSignInCompletion)(ASAuthorizationAppleIDCredential * _Null /// 返回:ASAuthorizationAppleIDProviderCredentialAuthorized/Revoked/NotFound - (void)checkCredentialStateWithCompletion:(void(^)(ASAuthorizationAppleIDProviderCredentialState state))completion; +/// 使用给定 userIdentifier 检查凭证状态(推荐:从服务端返回的 userIdentifier) +- (void)checkCredentialStateForUserID:(NSString *)userID completion:(void(^)(ASAuthorizationAppleIDProviderCredentialState state))completion; + /// 最近一次成功登录的 userIdentifier(已持久化) @property (nonatomic, readonly, nullable) NSString *storedUserIdentifier; diff --git a/keyBoard/Class/Manager/AppleSignInManager.m b/keyBoard/Class/Manager/AppleSignInManager.m index 1cb02b7..f92be26 100644 --- a/keyBoard/Class/Manager/AppleSignInManager.m +++ b/keyBoard/Class/Manager/AppleSignInManager.m @@ -63,14 +63,30 @@ static NSString * const kKBAppleUserIdentifierKey = @"com.company.keyboard.apple } } +// 使用指定的 userIdentifier 检查凭证状态(无需本地存储) +- (void)checkCredentialStateForUserID:(NSString *)userID completion:(void(^)(ASAuthorizationAppleIDProviderCredentialState state))completion { + if (!completion) return; + if (@available(iOS 13.0, *)) { + if (userID.length == 0) { + completion(ASAuthorizationAppleIDProviderCredentialNotFound); + return; + } + ASAuthorizationAppleIDProvider *provider = [ASAuthorizationAppleIDProvider new]; + [provider getCredentialStateForUserID:userID completion:^(ASAuthorizationAppleIDProviderCredentialState credentialState, NSError * _Nullable error) { + dispatch_async(dispatch_get_main_queue(), ^{ completion(credentialState); }); + }]; + } else { + completion(ASAuthorizationAppleIDProviderCredentialNotFound); + } +} + #pragma mark - 授权回调 (ASAuthorizationControllerDelegate) - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) { if (@available(iOS 13.0, *)) { ASAuthorizationAppleIDCredential *credential = authorization.credential; - // 持久化保存 userIdentifier,便于后续校验凭证状态 - NSString *userID = credential.user; - [self.class keychainSave:kKBAppleUserIdentifierKey value:userID]; + // 不在本地持久化 userIdentifier,统一交给服务端处理 + // NSString *userID = credential.user; // 如需本地缓存,请自行决定是否保存 if (self.completion) { self.completion(credential, nil); }