ios AppleID登录

iOS sign in with Apple 苹果ID登录

前期工作

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

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

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

选中singing & capability 选项,但是当前界面只展示了证书相关设置。

观察到下面有一行提示 add capablities by clicking the "+" button above.即添加+号可以添加需要的权限功能。

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

代码集成

导入头文件

#import <AuthenticationServices/AuthenticationServices.h>复制代码


ASAuthorizationControllerDelegate 处理数据回调

ASAuthorizationControllerPresentationContextProviding 设置上下文,管理视图弹出在哪里

初始化按钮

-(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];}复制代码

处理代理回调数据

///代理主要用于展示在哪里

-(ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){

returnself.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]]){                        //// Signinusing 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);

}

仅做笔记 ~ 更新排版

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容