美文网首页
ASAuthorizationAppleIDButton

ASAuthorizationAppleIDButton

作者: 破夕_____________ | 来源:发表于2019-10-22 14:03 被阅读0次
    if (@available(iOS 13.0, *)) {
         
            ASAuthorizationAppleIDButton *button = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeSignIn style:ASAuthorizationAppleIDButtonStyleBlack];
            button.frame = CGRectMake(0, 0, 60, 60);
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 30;
            [button addTarget:self
                       action:@selector(signInWithApple:)
             forControlEvents:UIControlEventTouchUpInside];
            button.center = self.view.center;
            [self.view addSubview:button];
            
        }
    
    - (void)signInWithApple:(ASAuthorizationAppleIDButton *)sender API_AVAILABLE(ios(13.0)){
        JMLog(@"Apple Login Click");
        
        if (@available(iOS 13.0, *)) {
            //基于用户的Apple ID授权用户,生成用户授权请求的一种机制
            ASAuthorizationAppleIDProvider *appleIDProvider = [ASAuthorizationAppleIDProvider new];
            if (_userId.length != 0) {
                //快速登录
                [appleIDProvider getCredentialStateForUserID:_userId completion:^(ASAuthorizationAppleIDProviderCredentialState credentialState, NSError * _Nullable error) {
                    
                }];
            }
            else {
                //授权请求AppleID
                ASAuthorizationAppleIDRequest *request = appleIDProvider.createRequest;
                [request setRequestedScopes:@[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail]];
                //由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
                ASAuthorizationController *controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
                //设置授权控制器通知授权请求的成功与失败的代理
                controller.delegate = self;
                //设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
                controller.presentationContextProvider = self;
                //在控制器初始化期间启动授权流
                [controller performRequests];
            }
        }
        else {
            // Fallback on earlier versions
        }
    }
    
    - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0))
    {
        if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]])       {
            ASAuthorizationAppleIDCredential *credential = authorization.credential;
            
            NSString *state = credential.state;
            NSString *userID = credential.user;
            NSPersonNameComponents *fullName = credential.fullName;
            NSString *email = credential.email;
            NSString *authorizationCode = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding]; // refresh token
            NSString *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding]; // access token
            ASUserDetectionStatus realUserStatus = credential.realUserStatus;
            
            JMLog(@"state: %@", state);
            JMLog(@"userID: %@", userID);
            JMLog(@"fullName: %@", fullName);
            JMLog(@"email: %@", email);
            JMLog(@"authorizationCode: %@", authorizationCode);
            JMLog(@"identityToken: %@", identityToken);
            JMLog(@"realUserStatus: %@", @(realUserStatus));
        }
    }
    
    - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0))
    {
        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;
        }
        JMLog(@"errorMsg:%@", errorMsg);
    }
    
    

    相关文章

      网友评论

          本文标题:ASAuthorizationAppleIDButton

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