美文网首页
apple登录

apple登录

作者: 无痕树人 | 来源:发表于2020-10-27 10:49 被阅读0次

    2020年4月后,有第三方平台登录(例如:微信登录)的app必须有apple的第三方登录

    1.xcode配置

    加上苹果第三方登录权限:
    TARGETS -- Signing &Capabilities 添加Sign In With APPle


    xcode配置.png

    2.苹果开发者中心 Identifiers 设置一下Identifiers权限

    苹果开发者中心配置.png

    3.上代码

    #import <AuthenticationServices/AuthenticationServices.h>
    
    ()<ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
    
    
    
    - (void)loginWithApple{
    
    if (@available(iOS 13.0, *)) {
        // Sign In With Apple Button
        ASAuthorizationAppleIDButton *appleIDBtn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeDefault style:ASAuthorizationAppleIDButtonStyleBlack];
        appleIDBtn.frame = CGRectMake(0, 0, 160, 40);  //自定义
        appleIDBtn.layer.cornerRadius = 5;
        appleIDBtn.layer.masksToBounds = YES;
        [appleIDBtn addTarget:self action:@selector(handleAuthorizationAppleIDButtonPress) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:appleIDBtn];
    }}
    
    
    -(void)handleAuthorizationAppleIDButtonPress {
    
    if (@available(iOS 13.0, *)) {
        
        // 基于用户的Apple ID授权用户,生成用户授权请求的一种机制
        ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
        // 创建新的AppleID 授权请求
        ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
        
        // 在用户授权期间请求的联系信息
        appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
        
        // 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
        ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];
        
        // 设置授权控制器通知授权请求的成功与失败的代理
        authorizationController.delegate = self;
        // 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
        authorizationController.presentationContextProvider = self;
        
        // 在控制器初始化期间启动授权流
        [authorizationController performRequests];
        
    }}
    
    pragma mark - ASAuthorizationControllerDelegate
    //@optional 授权成功地回调
    - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
    
    NSLog(@"授权完成:::%@", authorization.credential);
    NSLog(@"controller   -:%@", controller);
    NSLog(@"authorization    -:%@", authorization);
    
    if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {  
        
        // 用户登录使用ASAuthorizationAppleIDCredential
        ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
        NSString *user = appleIDCredential.user;
               
        NSString *identityToken = [[NSString alloc] initWithData:appleIDCredential.identityToken encoding:NSUTF8StringEncoding]; // access token
        
       //!!!!! 这里与自己服务交互  //
        
     
    }else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]) {   
    
        // Sign in using an existing iCloud Keychain credential.
        // 用户登录使用现有的密码凭证
        ASPasswordCredential *passwordCredential = authorization.credential;
        // 密码凭证对象的用户标识 用户的唯一标识
        NSString *userId = passwordCredential.user;
        // 密码凭证对象的密码
        NSString *password = passwordCredential.password;
        
        
    } else {
        NSLog(@"授权信息均不符");
    }}
    
    // 授权失败的回调
    - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
    // Handle error.
    
    NSLog(@"Handle error:%@", error);
    NSString *errorMsg = nil;
    
    switch(error.code) {
        case ASAuthorizationErrorCanceled:
            errorMsg = @"用户取消了授权请求";
            NSLog(@"errorMsg -   %@",errorMsg);
            break;
        
        case ASAuthorizationErrorFailed:
            errorMsg = @"授权请求失败";
            NSLog(@"errorMsg -   %@",errorMsg);
            break;
        
        case ASAuthorizationErrorInvalidResponse:
            errorMsg = @"授权请求响应无效";
            NSLog(@"errorMsg -   %@",errorMsg);
            break;
            
        case ASAuthorizationErrorNotHandled:
            errorMsg = @"未能处理授权请求";
            NSLog(@"errorMsg -   %@",errorMsg);
            break;
        
        case ASAuthorizationErrorUnknown:
            errorMsg = @"授权请求失败未知原因";
            NSLog(@"errorMsg -   %@",errorMsg);
            break;
        
        default:
            
            break;
            
    }}
    
    pragma mark - ASAuthorizationControllerPresentationContextProviding
    // 告诉代理应该在哪个window 展示内容给用户
    - (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
    // 返回window
    return self.view.window;}
    

    相关文章

      网友评论

          本文标题:apple登录

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