美文网首页iOS学习笔记
Sign in with Apple ID 苹果登录 ios13

Sign in with Apple ID 苹果登录 ios13

作者: 愤斗的小蚂蚁 | 来源:发表于2020-01-06 10:42 被阅读0次

    【1】使用第三方登录而没有使用Apple登录时审核被拒邮件内容如下:

    ### Guideline 4.8 - Design - Sign in with Apple
    
    We noticed that your app uses a third-party login service but does not offer Sign in with Apple.
    
    **Next Steps**
    
    To resolve this issue, please revise your app to offer Sign in with Apple as an equivalent login option.
    
    **Resources**
    
    To learn more, see the Sign in with Apple [Overview](https://developer.apple.com/sign-in-with-apple/).
    

    【2】苹果登录实现步骤与注意事项
    【注意】使用苹果登录首先需要在苹果开发者后台开启 App 的 Sign In with Apple 服务只有发布Appstore的应用才能使用苹果登录。企业版开发者账号不支持 Sign In with Apple (企业版开发者账号指的是用于企业内部分发App,不能用于发布 App Store 的账号,也就是价格为 299$ 的账号)

    官方Demo https://developer.apple.com/documentation/authenticationservices/adding_the_sign_in_with_apple_flow_to_your_app

    【图片指引】 appleid-001.png appleid-002.png appleid-003.png appleid-004.png appleid-005.png appleid-006.png appleid-007.png appleid-008.png

    【代码】

    #import <AuthenticationServices/AuthenticationServices.h>
    @interface LoginView ()<
    ASAuthorizationControllerDelegate,
    ASAuthorizationControllerPresentationContextProviding
    >
    @end
    @implementation LoginView
    #pragma mark - AppleIDLogin
    - (void) setup {
    
        if (@available(iOS 13.0, *)) {
    
            /*
            // 创建一个 provider
            ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
            UmUserModel *model = [UmUserModel getUmUserModel];
            if ( [AppleIDLogin isEqualToString:model.loginType] ) {
    
            }
            [appleIDProvider getCredentialStateForUserID:model.unionid completion:^(ASAuthorizationAppleIDProviderCredentialState credentialState, NSError * _Nullable error) {
    
              if ( credentialState == ASAuthorizationAppleIDProviderCredentialAuthorized ) {
    
              }
              else {
    
              }
            }];
            */
    
            ASAuthorizationAppleIDButton *signInButton = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeSignIn style:ASAuthorizationAppleIDButtonStyleBlack];
            signInButton.frame = self.view0.bounds;
            [self.view0 addSubview:signInButton];
            // 添加响应方法
            [signInButton addTarget:self action:@selector(signInButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    - (void) signInButtonClicked:(ASAuthorizationAppleIDButton *)signInButton  API_AVAILABLE(ios(13.0)){
        
        ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
        ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];    
        appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];// 要获取的内容
        
        // 登录使用的一种方式,后面单独讨论
    //    ASAuthorizationPasswordProvider *passwordProvider = [ASAuthorizationPasswordProvider new];
    //    ASAuthorizationPasswordRequest *passwordRequest = [passwordProvider createRequest];    
        
        // 系统提供的 Controller,必须使用,需要传入 requests 数组
        ASAuthorizationController *authController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];//, passwordRequest
        // 设置代理,接收登录成功/失败的回调
        authController.delegate = self;
        // 页面跳转相关的,通过一个代理方法传入一个 window
    
        authController.presentationContextProvider = self;
        // Controller 初始化期间,开始授权流程
        [authController performRequests];
    }
    
    #pragma mark - ASAuthorizationControllerDelegate
    - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error  API_AVAILABLE(ios(13.0)){
        // 登录失败
        NSLog(@"auth failed!, error:%@, code:%ld, description:%@", error, (long)error.code, error.description);
    }
    
    - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization  API_AVAILABLE(ios(13.0)){
        // 登录成功    
        if ( [authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]] ) {
            
            ASAuthorizationAppleIDCredential *credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
                    
            // 还有其他的属性
            NSLog(@"appleid auth success!, %@", authorization.description);       
           // 登录后具体操作,如数据保存,APP自己的登录等
        }
    }
    
    #pragma mark - ASAuthorizationControllerPresentationContextProviding
    - (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller  API_AVAILABLE(ios(13.0)){
        // 返回一个 window,present 登录界面需要用到
        return [UIApplication sharedApplication].delegate.window;
    }
    
    #pragma mark AppleIDLogin end
    @end
    

    相关文章

      网友评论

        本文标题:Sign in with Apple ID 苹果登录 ios13

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