Sign In with Apple
iOS 13新增功能之Sign In with Apple肯骇,可以通過AppleID在iOS設(shè)備上進行登錄等操作
蘋果登錄開發(fā)流程:
一、配置
1.需要在蘋果后臺打開Sign In with Apple選項担映,并且重新生成Profiles配置文件,并安裝到Xcode。
2.在開發(fā)者賬號中的證書選項中洋机,單擊Configure按鈕浇冰,選擇你創(chuàng)建的Primary App ID贬媒,保存之后,下載p8文件肘习。
二际乘、代碼
- 導(dǎo)入系統(tǒng)頭文件#import <AuthenticationServices/AuthenticationServices.h>,
添加Sign In with Apple登錄按鈕漂佩,設(shè)置ASAuthorizationAppleIDButton相關(guān)布局脖含,并添加按鈕點擊響應(yīng)
if (@available(iOS 13.0, *)) {
ASAuthorizationAppleIDButton *appleLoginBtn = [[ASAuthorizationAppleIDButton alloc] initWithAuthorizationButtonType:ASAuthorizationAppleIDButtonTypeSignIn authorizationButtonStyle:ASAuthorizationAppleIDButtonStyleBlack];
appleLoginBtn.frame = CGRectMake(0, 0, 50, 50);
appleLoginBtn.layer.cornerRadius = 5;
appleLoginBtn.layer.masksToBounds = YES;
[appleLoginBtn addTarget:self action:@selector(appleLogin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:appleLoginBtn];
} else {
// Fallback on earlier versions
}
事件
- 獲取授權(quán)碼
- (void)appleLogin {
if (@available(iOS 13.0, *)) {
ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
// 用戶授權(quán)請求的聯(lián)系信息
appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];
// 設(shè)置授權(quán)控制器通知授權(quán)請求的成功與失敗的代理
authorizationController.delegate = self;
// 設(shè)置提供 展示上下文的代理,在這個上下文中 系統(tǒng)可以展示授權(quán)界面給用戶
authorizationController.presentationContextProvider = self;
// 在控制器初始化期間啟動授權(quán)流
[authorizationController performRequests];
} else {
NSLog(@"該系統(tǒng)版本不可用Apple登錄");
}
}