iOS. 通过appleId进行苹果第三方登录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS. 通过appleId进行苹果第三方登录相关的知识,希望对你有一定的参考价值。

参考技术A 2020年4月后,有第三方平台登录的app必须有苹果的第三方登录

开发者账号的app绑定的identifiers 中选中

在Xcode 中的

添加

在登录界面添加 #import <AuthenticationServices/AuthenticationServices.h>

    if ( @available (ios13.0, *))

        // Sign In With Apple Button

        ASAuthorizationAppleIDButton *appleIDBtn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeDefault style:ASAuthorizationAppleIDButtonStyleWhite];

        appleIDBtn.frame=  CGRectMake(50,100,100,40);

        appleIDBtn.layer.cornerRadius=5;

        appleIDBtn.layer.masksToBounds= YES ;

        [appleIDBtnaddTarget: self action: @selector (handleAuthorizationAppleIDButtonPress)forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:appleIDBtn];

   

//苹果第三方登录

-( void )handleAuthorizationAppleIDButtonPress

    if ( @available (iOS13.0, *))

        // 基于用户的Apple ID授权用户,生成用户授权请求的一种机制

        ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];

        // 创建新的AppleID 授权请求

        ASAuthorizationAppleIDRequest*appleIDRequest = [appleIDProvidercreateRequest];

        // 在用户授权期间请求的联系信息

        appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];

        // 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器

        ASAuthorizationController*authorizationController = [[ASAuthorizationControlleralloc]initWithAuthorizationRequests:@[appleIDRequest]];

        // 设置授权控制器通知授权请求的成功与失败的代理

        authorizationController.delegate= self ;

        // 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户

        authorizationController.presentationContextProvider= self ;

        // 在控制器初始化期间启动授权流

        [authorizationControllerperformRequests];

   



// 如果存在iCloud Keychain 凭证或者AppleID 凭证提示用户  

//这个方法在viewDidAppear里添加

- ( void )perfomExistingAccountSetupFlows

    NSLog(@"///已经认证过了/////");

    if ( @available (iOS13.0, *))

        // 基于用户的Apple ID授权用户,生成用户授权请求的一种机制

        ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];

        // 授权请求AppleID

        ASAuthorizationAppleIDRequest*appleIDRequest = [appleIDProvidercreateRequest];

        // 为了执行钥匙串凭证分享生成请求的一种机制

        ASAuthorizationPasswordProvider *passwordProvider = [[ASAuthorizationPasswordProvider alloc] init];

        ASAuthorizationPasswordRequest*passwordRequest = [passwordProvidercreateRequest];

        // 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器

        ASAuthorizationController*authorizationController = [[ASAuthorizationControlleralloc]initWithAuthorizationRequests:@[appleIDRequest, passwordRequest]];

        // 设置授权控制器通知授权请求的成功与失败的代理

        authorizationController.delegate= self ;

        // 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户

        authorizationController.presentationContextProvider= self ;

        // 在控制器初始化期间启动授权流

        [authorizationControllerperformRequests];

   



#pragma mark - delegate

//@optional 授权成功地回调

- ( 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;

        NSString*user = appleIDCredential.user;

        NSString*familyName = appleIDCredential.fullName.familyName;

        NSString*givenName = appleIDCredential.fullName.givenName;

        NSString*email = appleIDCredential.email;

        NSString *authorizationCode = [[NSString alloc] initWithData:appleIDCredential.authorizationCode encoding:NSUTF8StringEncoding]; // refresh token

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

        ASUserDetectionStatusrealUserStatus = appleIDCredential.realUserStatus;

//        NSData *identityToken = appleIDCredential.identityToken;

//        NSData *authorizationCode = appleIDCredential.authorizationCode;

        // Create an account in your system.

        // For the purpose of this demo app, store the userIdentifier in the keychain.

        //  需要使用钥匙串的方式保存用户的唯一信息

//        [mStr appendString:user];

//        [mStr appendString:@"\n"];

//        [mStr appendString:familyName];

//        [mStr appendString:@"\n"];

//        [mStr appendString:givenName];

//        [mStr appendString:@"\n"];

//        [mStr appendString:email];

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

//        [mStr appendString:@"\n"];

    else if ([authorization.credentialisKindOfClass:[ASPasswordCredentialclass]])

        // Sign in using an existing iCloud Keychain credential.

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

        ASPasswordCredential*passwordCredential = authorization.credential;

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

        NSString*user = passwordCredential.user;

        // 密码凭证对象的密码

        NSString*password = passwordCredential.password;

        [mStrappendString:user];

        [mStrappendString:@"\n"];

        [mStrappendString:password];

        [mStrappendString:@"\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 ;

   

//    NSMutableString *mStr = [_appleIDInfoLabel.text mutableCopy];

//    [mStr appendString:@"\n"];

//    [mStr appendString:errorMsg];

//    [mStr appendString:@"\n"];



// 告诉代理应该在哪个window 展示内容给用户

- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller

    NSLog(@"88888888888");

    // 返回window

    return self .view.window;



后台进行验证信息,返回验证信息过期,我一直在找原因,找了一天,后来发现是后台code码整错了,-1代表验证通过,验证不通过是重新调用一下handleAuthorizationAppleIDButtonPress这个方法就可以,我甚至去看官方文档,也没有说重新获取token或者删除账号的方法

以上是关于iOS. 通过appleId进行苹果第三方登录的主要内容,如果未能解决你的问题,请参考以下文章

iOS 苹果登录(第三方登录)

苹果手机怎么下载应用宝?

iOS 第三方登录之苹果登录(sign in with Apple)

苹果手机为啥登陆不了icloud?

iOS实现苹果第三方登录功能 - Sign in with apple

iOS实现苹果第三方登录功能:Sign in with Apple