美文网首页iOS三方登录
第三方登录 Sign in with Apple

第三方登录 Sign in with Apple

作者: 小涛_78e8 | 来源:发表于2020-05-18 16:03 被阅读0次

首先需要登录开发者中心,将Identifiers打开Sign in with Apple权限,然后从新更新描述文件Profiles。


image.png

然后Xcode在这里添加Sign in with Apple,再然后就可以去敲代码了。


image.png
导入头文件#import <AuthenticationServices/AuthenticationServices.h>,再加上代理ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding
//系统提供的按钮(如果用系统提供按钮,建议第三方登录做成这样,别问为什么,因为被拒的多了)
image.png

if (@available(iOS 13.0, *)) {
// Sign In With Apple Button
ASAuthorizationAppleIDButton *appleIDBtn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeDefault style:ASAuthorizationAppleIDButtonStyleWhite];
[self.view addSubview:appleIDBtn];
[appleIDBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(200);
make.height.mas_equalTo(50);
make.bottom.equalTo(line.mas_top).offset(-20);
make.centerX.equalTo(line);
}];
appleIDBtn.layer.cornerRadius = 25;
appleIDBtn.layer.borderWidth = 1;
appleIDBtn.layer.borderColor = [UIColor grayColor].CGColor;
[appleIDBtn addTarget:self action:@selector(didAppleIDBtnClicked) forControlEvents:UIControlEventTouchUpInside];

}

或者选择自定义,图片样式建议这样,或者圆角、矩形。


image.png

苹果官方给出样式


image.png
  • (void)didAppleIDBtnClicked{

    if (@available(iOS 13.0, *)) {
    [self signInWithAppleNative];
    } else {
    // Fallback on earlier versions
    }
    }
    -(void)signInWithAppleNative API_AVAILABLE(ios(13.0)){
    ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
    ASAuthorizationAppleIDRequest *authAppleIDRequest = [provider createRequest];
    // 在用户授权期间请求的联系信息
    authAppleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];

// ASAuthorizationPasswordRequest *passwordRequest = [[ASAuthorizationPasswordProvider new] createRequest];

NSMutableArray <ASAuthorizationRequest *>* array = [NSMutableArray arrayWithCapacity:2];
if (authAppleIDRequest) {
    [array addObject:authAppleIDRequest];
}

// if (passwordRequest) {
// [array addObject:passwordRequest];
// }
NSArray <ASAuthorizationRequest > requests = [array copy];

// 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:requests];
authorizationController.delegate = self;
// 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
authorizationController.presentationContextProvider = self;

// 在控制器初始化期间启动授权流
[authorizationController performRequests];
}

  • (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization{

    NSLog(@"授权完成:::%@", authorization.credential);

    NSLog(@"%s", FUNCTION);

    NSLog(@"%@", controller);

    NSLog(@"%@", authorization);

    // 测试配置UI显示

    NSMutableString *mStr = [NSMutableString string];

    if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {

      // 用户登录使用ASAuthorizationAppleIDCredential
    
      ASAuthorizationAppleIDCredential*appleIDCredential = authorization.credential;
    

//这个值试了多次发现没有变
NSStringuser = appleIDCredential.user;
//下面4个值只有第一次能获取到,以后每次获取的都是空值
NSString
familyName = appleIDCredential.fullName.familyName;
NSString*nickname = appleIDCredential.fullName.nickname;

    NSString*givenName = appleIDCredential.fullName.givenName;

    NSString*email = appleIDCredential.email;

//下面2个每次都会变
NSString *authorizationCode = [[NSString alloc] initWithData:appleIDCredential.authorizationCode encoding:NSUTF8StringEncoding]; // refresh token

    NSString *identityToken = [[NSString alloc] initWithData:appleIDCredential.identityToken encoding:NSUTF8StringEncoding]; // access token

   //自己用哪个值,根据实际情况来定,也可以将 identityToken存钥匙串保证唯一性。

}else if([authorization.credential isKindOfClass:[ASPasswordCredential class]]){

    // Sign in using an existing iCloud Keychain credential.

    // 用户登录使用现有的密码凭证

    ASPasswordCredential*passwordCredential = authorization.credential;

    // 密码凭证对象的用户标识 用户的唯一标识

    NSString*user = passwordCredential.user;

    // 密码凭证对象的密码

    NSString*password = passwordCredential.password;

    [mStr appendString:user];

    [mStr appendString:@"\n"];

    [mStr appendString:password];

    [mStr appendString:@"\n"];

    NSLog(@"mStr:::%@", mStr);

}else{

    NSLog(@"授权信息均不符");

    mStr = [@"授权信息均不符"copy];

}

}

// 授权失败的回调

  • (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error{

    // Handle error.

    NSLog(@"Handle error:%@", error);

    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;
    
      default:
    
          break;
    

    }

}

相关文章

网友评论

    本文标题:第三方登录 Sign in with Apple

    本文链接:https://www.haomeiwen.com/subject/ernuohtx.html