Sign In with Apple
iOS 13新增功能之Sign In with Apple,可以通过AppleID在iOS设备上进行登录等操作
苹果登录开发流程:
一、配置
1.需要在苹果后台打开Sign In with Apple选项,并且重新生成Profiles配置文件,并安装到Xcode。
2.在开发者账号中的证书选项中,单击Configure按钮,选择你创建的Primary App ID,保存之后,下载p8文件。
二、代码
- 导入系统头文件#import <AuthenticationServices/AuthenticationServices.h>,
添加Sign In with Apple登录按钮,设置ASAuthorizationAppleIDButton相关布局,并添加按钮点击响应
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
}
事件
- 获取授权码
- (void)appleLogin {
if (@available(iOS 13.0, *)) {
ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
// 用户授权请求的联系信息
appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];
// 设置授权控制器通知授权请求的成功与失败的代理
authorizationController.delegate = self;
// 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
authorizationController.presentationContextProvider = self;
// 在控制器初始化期间启动授权流
[authorizationController performRequests];
} else {
NSLog(@"该系统版本不可用Apple登录");
}
}