一、什么时候需要集成苹果登录?
有第三方登录时便需要集成苹果登录,如含微信登录功能;
二、苹果登录集成步骤:
1、进入https://developer.apple.com/accoun开发者中心找到对应的Bundle ID,勾选Sign In with Apple
2、Xcode--->targets--->对应项目--->添加Sign In with Apple,添加完后会自动生成一个entitlements
3、代码集成:
(1)、懒加载创建苹果登录按钮:
- (ASAuthorizationAppleIDButton *)appleLoginBtn API_AVAILABLE(ios(13.0)){
if (!_appleLoginBtn) {
_appleLoginBtn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeDefault style:ASAuthorizationAppleIDButtonStyleBlack];
[_appleLoginBtn addTarget:self action:@selector(signInWithApple) forControlEvents:UIControlEventTouchUpInside];
_appleLoginBtn.layer.cornerRadius = 25;
_appleLoginBtn.layer.masksToBounds = YES;
}
return _appleLoginBtn;
}
PS:踏坑预警:ASAuthorizationAppleIDButtonStyleBlack这个属性为黑底白字样式,如果你的背景色是浅色的就得使用黑底白字,那个黑色边线框的不可以,会被拒。反则反之。喊累被拒图如下:
(2)点击事件:
#pragma mark - click action
/**
* 苹果登录
*/
- (void)signInWithApple {
if (@available(iOS 13.0, *)) {
ASAuthorizationAppleIDProvider * appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
ASAuthorizationAppleIDRequest * authAppleIDRequest = [appleIDProvider createRequest];
NSMutableArray <ASAuthorizationRequest *> * array = [NSMutableArray arrayWithCapacity:2];
if (authAppleIDRequest) {
[array addObject:authAppleIDRequest];
}
NSArray <ASAuthorizationRequest *> * requests = [array copy];
ASAuthorizationController * authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:requests];
authorizationController.delegate = self;
authorizationController.presentationContextProvider = self;
[authorizationController performRequests];
}
}
(3)代理方法:
#pragma mark - ASAuthorizationController delegate
// 授权成功
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
NSLog(@"授权成功");
if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
ASAuthorizationAppleIDCredential * credential = authorization.credential;
// 苹果用户唯一标识符,该值在同一个开发者账号下的所有 App 下是一样的,开发者可以用该唯一标识符与自己后台系统的账号体系绑定起来。
NSString * userID = credential.user;
// 苹果用户信息 如果授权过,可能无法再次获取该信息
NSPersonNameComponents * fullName = credential.fullName;
NSString * email = credential.email;
// 服务器验证需要使用的参数
NSString * authorizationCode = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding];
NSString * identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
// 用于判断当前登录的苹果账号是否是一个真实用户,取值有:unsupported、unknown、likelyReal
ASUserDetectionStatus realUserStatus = credential.realUserStatus;
NSLog(@"userID: %@", userID);
NSLog(@"fullName: %@", fullName);
NSLog(@"email: %@", email);
NSLog(@"authorizationCode: %@", authorizationCode);
NSLog(@"identityToken: %@", identityToken);
NSLog(@"realUserStatus: %@", @(realUserStatus));
NSDictionary *dic = @{@"apple_user":userID,@"identity_token":identityToken};
//这是我拿到苹果返回的user和token去自己服务器请求验证方法
[self loginAppleLoginWithParams:dic];
}
}
PS:踏坑预警:验证成功后不能强制绑定手机号,否则会被拒。如果业务上必须需要手机号和用户信息的,可以做个开关模式避审,否则别执拗,不然是过不了审的。