iOS sign in with Apple 苹果ID登录

一.项目配置

1.进去到在苹果开发者网站,在对应的app id下面开通sign in with Apple 的权限。这点类似于推送功能。如下图

iOS sign in with Apple 苹果ID登录_第1张图片

apple1.png

注:右边编辑选项为分组功能,如果设置了分组可以使用一下。

2.在项目工程中添加响应的权限功能设置,(类似于推送)

iOS sign in with Apple 苹果ID登录_第2张图片

apple2.png

1.选中singing & capability 选项,但是当前界面只展示了证书相关设置。
2.观察到下面有一行提示 add capablities by clicking the "+" button above.即添加+号可以添加需要的权限功能。

 

.

iOS sign in with Apple 苹果ID登录_第3张图片

apple3.png

3.选中对应的功能选项即可。到此前期功能已经全部做完了。想删除掉点击右边的x即可.

 

iOS sign in with Apple 苹果ID登录_第4张图片

apple4.png

二.代码集成

1.导入头文件

 

#import 

2.遵循代理

 


   -- ASAuthorizationControllerDelegate 处理数据回调
    --ASAuthorizationControllerPresentationContextProviding 设置上下文,管理视图弹出在哪里

3.初始化按钮

 

-(void)configUI{
    if (@available(iOS 13.0, *)) {
        self.authorizationButton = [[ASAuthorizationAppleIDButton alloc]init];
        [self.authorizationButton addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
        self.authorizationButton.center = self.view.center;
        [self.view addSubview:self.authorizationButton];
    } else {
        // Fallback on earlier versions
    }
}

-(void)click API_AVAILABLE(ios(13.0)){
    ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc]init];
    ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest];
    request.requestedScopes = @[ASAuthorizationScopeFullName,ASAuthorizationScopeEmail];
    ASAuthorizationController *auth = [[ASAuthorizationController alloc]initWithAuthorizationRequests:@[request]];
    auth.delegate = self;
    auth.presentationContextProvider = self;
    [auth performRequests];
}

4.处理代理回调数据

 

///代理主要用于展示在哪里
-(ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
    return self.view.window;
}


-(void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
        if([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]){
            ASAuthorizationAppleIDCredential *apple = authorization.credential;
            ///将返回得到的user 存储起来
            NSString *userIdentifier = apple.user;
            NSPersonNameComponents *fullName = apple.fullName;
            NSString *email = apple.email;
            //用于后台像苹果服务器验证身份信息
            NSData *identityToken = apple.identityToken;
            
            
            NSLog(@"%@%@%@%@",userIdentifier,fullName,email,identityToken);
        }else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]){
            
             Sign in using an existing iCloud Keychain credential.
            ASPasswordCredential *pass = authorization.credential;
            NSString *username = pass.user;
            NSString *passw = pass.password;
            
        }
    
}

///回调失败
-(void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
    NSLog(@"%@",error);
}

5.在回调的代理中 做一些UI变化 提示信息。或者状态通知等等。



转载链接:https://www.jianshu.com/p/2bbd493aec6d

你可能感兴趣的:(ios,objective-c)