前言
如果要上架的應用集成了三方登錄,那么在審核時,蘋果會強制要求應用也要集成蘋果登錄,如果應用沒有集成一般情況下都會被審核團隊給打回來,
蘋果集成登錄
首先,你需要在開發者中心,找到你的應用,勾選上 Sign in with Apple

勾選并保存好后, 打開Xcode, 找到專案中的Signing&Capabilities 并添加sign in with apple,

完成上面的配置以后,就可以寫代碼集成了
在進行代碼集成前,需要先匯入一個框架 AuthenticationServices.framework
import <AuthenticationServices/AuthenticationServices.h>
并遵守ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding協議
如果是自定義蘋果按鈕樣式, 直接處理按鈕事件
/**
蘋果登錄
*/
- (void)signInWithApple {
if (@available(iOS 13.0, *)) {
ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
ASAuthorizationAppleIDRequest *request = [provider createRequest];
request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
ASAuthorizationController *vc = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
vc.delegate = self;
vc.presentationContextProvider = self;
[vc performRequests];
}
}
點擊后會觸發按鈕事件, 接著調起蘋果登錄驗證,驗證通過后,走驗證回呼方法
#pragma mark 蘋果集成登錄
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization NS_SWIFT_NAME(authorizationController(controller:didCompleteWithAuthorization:)) API_AVAILABLE(ios(13.0)){
if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
ASAuthorizationAppleIDCredential *credential = authorization.credential;
NSLog(@"credential = %@",credential);
NSString *state = credential.state;
NSString *userID = credential.user;
NSPersonNameComponents *fullName = credential.fullName;
NSString *email = credential.email;
NSString *authorizationCode = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding]; // 驗證 token
NSString *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding]; // 用戶 token
ASUserDetectionStatus realUserStatus = credential.realUserStatus;
NSArray *authorizedScopes = credential.authorizedScopes;
NSLog(@"state: %@\nuserID: %@\nfullName: %@\nemail: %@\nauthorizationCode: %@\nidentityToken: %@\nrealUserStatus: %@\nauthorizedScopes: %@",
state,
userID,
fullName,
email,
authorizationCode,
identityToken,
@(realUserStatus),
authorizedScopes);
//蘋果登錄,傳遞認證的token
[self loginWithAppleToken:token];
}
}
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error NS_SWIFT_NAME(authorizationController(controller:didCompleteWithError:)) API_AVAILABLE(ios(13.0)){
NSString *errorMsg = nil;
switch (error.code) {
case ASAuthorizationErrorCanceled:
errorMsg = @"用戶取消了授權請求";
break;
case ASAuthorizationErrorFailed:
errorMsg = @"授權請求失敗";
break;
case ASAuthorizationErrorInvalidResponse:
errorMsg = @"授權請求回應無效";
break;
case ASAuthorizationErrorNotHandled:
errorMsg = @"未能處理授權請求";
break;
case ASAuthorizationErrorUnknown:
errorMsg = @"授權請求失敗未知原因";
break;
}
DDLogDebug(@"蘋果授權失敗:%@",errorMsg);
}
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
return kAppDelegate.window;
}
蘋果授權驗證通過后,會獲取到回傳的Token, 客戶端拿著這個token 去呼叫服務器Api 來校驗, 校驗通過后處理用戶登錄的邏輯就可以了,
蘋果圖示的制作要求
詳見:https://zhanglei.blog.csdn.net/article/details/121492572
本文來自博客園,作者:reyzhang,轉載請注明原文鏈接:https://www.cnblogs.com/reyzhang/p/16199135.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/472416.html
標籤:iOS
下一篇:iOS Widget
