iOS开发 项目中集成Apple 登录

当你项目中使用了第三方登录时,则必须集成Apple登录。
第一步:先在项目中设置需要的配置;

1.登录你的开发者账号,在
截屏2020-03-1014.10.20.png
中找到你项目对应的identifiers,把sign in with apple 勾选保存。
截屏2020-03-1014.12.36.png

2.在项目中的Signing&Capabilities中添加sign in with apple。

完成以上就可以实现代码了。
代码实现:
在登录的文件中导入

import 并遵守ASAuthorizationControllerDelegate协议,

#pragma mark --- appid登录
- (void)didAppleButton {
    if (@available(iOS 13.0, *)) {
        // 基于用户AppleID的授权,生成用户授权请求的一种机制
        ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
        // 创建新的AppleID授权请求
        ASAuthorizationAppleIDRequest *request = [provider createRequest];
        // 在用户授权期间请求的联系信息
        request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
        // 由 ASAuthorizationAppleIDProvider 创建的授权请求来管理 授权请求控制器
        ASAuthorizationController *authController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
        authController.delegate = self;
        [authController performRequests];
    }
    
}

#pragma mark - ASAuthorizationControllerDelegate

/// Apple登录授权出错
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
    NSLog(@"Apple登录_错误信息: %@", error.localizedDescription);
    
    NSInteger code = error.code;
    if (code == ASAuthorizationErrorUnknown) { // 授权请求未知错误
        NSLog(@"Apple登录_授权请求未知错误");
    } else if (code == ASAuthorizationErrorCanceled) { // 授权请求取消了
        NSLog(@"Apple登录_授权请求取消了");
    } else if (code == ASAuthorizationErrorInvalidResponse) { // 授权请求响应无效
        NSLog(@"Apple登录_授权请求响应无效");
    } else if (code == ASAuthorizationErrorNotHandled) { // 授权请求未能处理
        NSLog(@"Apple登录_授权请求未能处理");
    } else if (code == ASAuthorizationErrorFailed) { // 授权请求失败
        NSLog(@"Apple登录_授权请求失败");
    }
}

/// Apple登录授权成功
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
    Class credentialClass = [authorization.credential class];
    if (credentialClass == [ASAuthorizationAppleIDCredential class]) {
        // 用户登录使用的是: ASAuthorizationAppleIDCredential,授权成功后可以取到苹果返回的全部数据,然后再与后台交互
        ASAuthorizationAppleIDCredential *credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
        
        NSString *userID = credential.user;
        NSString *state = credential.state;
        NSArray *authorizedScopes = credential.authorizedScopes;
        // refresh_token
        NSString *authorizationCode = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding];
        // access_token
        NSString *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
        NSString *email = credential.email;
        NSPersonNameComponents *fullName = credential.fullName;
        ASUserDetectionStatus realUserStatus = credential.realUserStatus;
        
        NSLog(@"Apple登录_1_user: %@", userID);
        NSLog(@"Apple登录_4_authorizationCode: %@", authorizationCode);
        NSLog(@"Apple登录_5_identityToken: %@", identityToken);
        NSLog(@"Apple登录_6_email: %@", email);
        NSLog(@"Apple登录_7_fullName.givenName: %@", fullName.givenName);
        NSLog(@"Apple登录_7_fullName.familyName: %@", fullName.familyName);
        NSLog(@"Apple登录_8_realUserStatus: %ld", realUserStatus);
        //这里我只用到了userID,email,[NSString stringWithFormat:@"%@%@", fullName.familyName, fullName.givenName]
       //接下来就调用自己服务器接口
    } else if (credentialClass == [ASPasswordCredential class]) {
        // 用户登录使用的是: 现有密码凭证
        ASPasswordCredential *credential = (ASPasswordCredential *)authorization.credential;
        NSString *user = credential.user; // 密码凭证对象的用户标识(用户的唯一标识)
        NSString *password = credential.password;
        NSLog(@"Apple登录_现有密码凭证: %@, %@", user, password);
    }
}

以上就是代码实现。
用户可以手动解除APPID和APP的关联所以要在登录成功后获取当前用户的apple授权登录状态。
在AppDelegate中的 启动方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中实现如下:

 if (@available(iOS 13.0, *)) {
        [appleInstance appleLoad];
           NSString *appleUserID = appleInstance.appleUserId;
           NSLog(@"Apple登录_本地存储的AppleUserID: %@", appleUserID);
           if (IsNull_Str(appleUserID)) {//为空
               return;
           }

           ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
           [appleIDProvider getCredentialStateForUserID:appleUserID completion:^(ASAuthorizationAppleIDProviderCredentialState credentialState, NSError * _Nullable error) {
               switch (credentialState) {
                   case ASAuthorizationAppleIDProviderCredentialAuthorized:
                       // Apple ID credential is valid
                       NSLog(@"Apple登录_Apple ID credential is valid");
                       break;
                       
                   case ASAuthorizationAppleIDProviderCredentialRevoked:
                       // Apple ID Credential revoked, handle unlink
                       NSLog(@"Apple登录_Apple ID Credential revoked, handle unlink");
                       [self unbindApple];  //跟自己服务器解绑
                       break;
                       
                   case ASAuthorizationAppleIDProviderCredentialNotFound:
                       // Apple ID Credential not found, show login UI
                       NSLog(@"Apple登录_Apple ID Credential not found, show login UI");
                       break;
                       
                   case ASAuthorizationAppleIDProviderCredentialTransferred:
                       NSLog(@"Apple登录_Apple ID Credential transferred");
                       break;
               }
           }];
        }

你可能感兴趣的:(iOS开发 项目中集成Apple 登录)