美文网首页
iOS Sign In with Apple

iOS Sign In with Apple

作者: iOS小武哥 | 来源:发表于2020-08-06 17:57 被阅读0次

    什么是Sign In with Apple? (简称:SIwA)

    苹果在WWDC2019推出了Sign In With Apple, 即用户通过端上的Apple ID就可以登录第三方应用。目的是为用户提供一种快速,安全且隐私友好的方式来设置帐户并开始使用您的服务。这次苹果提供的Sign In With Apple快捷登录,也是通过这个跨平台的功能使得在这个生态环境中的应用操作更加方便简洁。 前提是操作系统在iOS13及以上.需要判断一下,在iOS13以上在创建按钮,否则不创建!

    1.我这是新建一个工程,首先需要在工程中配置一下Sign In with Apple,如下图:
    WechatIMG16.jpeg

    当你操作完第4步的时候就会自动生成5和6!

    2.代码实现:(需要用到系统库)并遵循协议
    #import "RootViewController.h"
    #import <AuthenticationServices/AuthenticationServices.h>
    
    @interface RootViewController ()<ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>
    
    @end
    
    2.1 根据你们需求在相应的地方创建按钮:
    //创建Sign In with Apple按钮
    if (@available(iOS 13.0, *)) {
            //按钮类型: 通过ASAuthorizationAppleIDButtonType:设置
           //按钮样式: 通过ASAuthorizationAppleIDButtonStyle:设置
            ASAuthorizationAppleIDButton *appidBtn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeSignIn style:ASAuthorizationAppleIDButtonStyleWhiteOutline];
            //如果不设置Frame,按钮宽高默认值为 {width:130, height:30}
            appidBtn.frame = CGRectMake(100, 200, 150, 50);
            [appidBtn addTarget:self action:@selector(user_clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            appidBtn.cornerRadius = 25;
            [self.view addSubview:appidBtn];
        } else {
            // Fallback on earlier versions
        }
    
    2.2 实现按钮点击事件:
    #pragma mark -- 实现按钮点击事件
    - (void)user_clickBtnAction:(ASAuthorizationAppleIDButton *)btn API_AVAILABLE(ios(13.0)){
        //基于用户的Apple ID授权用户,生成用户授权请求的一种机制
        ASAuthorizationAppleIDProvider *appleIdProvider = [[ASAuthorizationAppleIDProvider alloc] init];
        //创建授权请求
        ASAuthorizationAppleIDRequest *request = appleIdProvider.createRequest;
        //在用户授权期间请求联系信息
        request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
        // 创建管理授权请求的控制器
        ASAuthorizationController *controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
        //设置代理
        controller.delegate = self;
        controller.presentationContextProvider = self;
        //发起请求
        [controller performRequests];
    }
    
    2.3 实现协议事件:
    #pragma mark -- 授权成功
    -(void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization
    API_AVAILABLE(ios(13.0)){
        if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
            ASAuthorizationAppleIDCredential *credential = authorization.credential;
            NSLog(@"user == %@ --- identityToken == %@", credential.user, credential.identityToken);
    
        }
        else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]])
        {
            ASPasswordCredential *psdCredential = authorization.credential;
            NSLog(@"user == %@ --- identityToken == %@", psdCredential.user, psdCredential.password);
        }
        else
        {
            NSLog(@"授权信息不符");
        }
    }
    
    #pragma mark -- 授权失败
    -(void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error
    API_AVAILABLE(ios(13.0)){
        NSLog(@"error = %@", error);
        switch (error.code) {
            case ASAuthorizationErrorCanceled:
                NSLog(@"用户取消请求!");
                break;
            case ASAuthorizationErrorFailed:
                NSLog(@"授权请求失败!");
            break;
            
            case ASAuthorizationErrorInvalidResponse:
                NSLog(@"授权请求响应无效!");
            break;
                
            case ASAuthorizationErrorNotHandled:
                NSLog(@"未处理授权响应!");
                break;
                
            case ASAuthorizationErrorUnknown:
                NSLog(@"未知原因!");
                break;
                
            default:
                break;
        }
    }
    -(ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller  API_AVAILABLE(ios(13.0)){
        return [UIApplication sharedApplication].windows.firstObject;
    }
    
    
    2.4 此过程需要在网络畅通情况下使用,否则会失败.将获取到的user等信息告诉后台,然后后台根据苹果那边后台API进行对接获取用户信息!
    2.5 完成上面的工作,效果如下:
    WechatIMG31.jpeg
    2.6 通过上图我们可以看到按钮文字: Sign in with Apple 我们希望现实成中文,那就添加本地化语言就可以了.然后效果图如下:
    WechatIMG32.jpeg

    相关文章

      网友评论

          本文标题:iOS Sign In with Apple

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