123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // SignInApple.m
- // SignInAppleDemo
- //
- // Created by Yostar on 2019/11/25.
- // Copyright © 2019 Yostar. All rights reserved.
- //
- #import "SignInApple.h"
- #import <AuthenticationServices/AuthenticationServices.h>
- #import "YostarKeychain.h"
- #define KEYCHAIN_IDENTIFIER(a) ([NSString stringWithFormat:@"%@_%@",[[NSBundle mainBundle] bundleIdentifier],a])
- @interface SignInApple () <ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
- @end
- @implementation SignInApple
- // 处理授权
- - (void)handleAuthorizationAppleIDButtonPress{
- NSLog(@"////////");
-
- if (@available(iOS 13.0, *)) {
- // 基于用户的Apple ID授权用户,生成用户授权请求的一种机制
- ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
- // 创建新的AppleID 授权请求
- ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
- // 在用户授权期间请求的联系信息
- appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
- // 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
- ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];
- // 设置授权控制器通知授权请求的成功与失败的代理
- authorizationController.delegate = self;
- // 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
- authorizationController.presentationContextProvider = self;
- // 在控制器初始化期间启动授权流
- [authorizationController performRequests];
- }else{
- // 处理不支持系统版本
- NSLog(@"该系统版本不可用Apple登录");
- }
- }
- // 如果存在iCloud Keychain 凭证或者AppleID 凭证提示用户
- - (void)perfomExistingAccountSetupFlows{
- NSLog(@"///已经认证过了/////");
-
- if (@available(iOS 13.0, *)) {
- // 基于用户的Apple ID授权用户,生成用户授权请求的一种机制
- ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
- // 授权请求AppleID
- ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
- // 为了执行钥匙串凭证分享生成请求的一种机制
- ASAuthorizationPasswordProvider *passwordProvider = [[ASAuthorizationPasswordProvider alloc] init];
- ASAuthorizationPasswordRequest *passwordRequest = [passwordProvider createRequest];
- // 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
- ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest, passwordRequest]];
- // 设置授权控制器通知授权请求的成功与失败的代理
- authorizationController.delegate = self;
- // 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
- authorizationController.presentationContextProvider = self;
- // 在控制器初始化期间启动授权流
- [authorizationController performRequests];
- }else{
- // 处理不支持系统版本
- NSLog(@"该系统版本不可用Apple登录");
- }
- }
- #pragma mark - delegate
- //@optional 授权成功地回调
- - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
- NSLog(@"授权完成:::%@", authorization.credential);
- NSLog(@"%s", __FUNCTION__);
- NSLog(@"%@", controller);
- NSLog(@"%@", authorization);
-
- if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
- // 用户登录使用ASAuthorizationAppleIDCredential
- ASAuthorizationAppleIDCredential *appleIDCredential = (ASAuthorizationAppleIDCredential *)authorization.credential;
- NSString *user = appleIDCredential.user;
- // 使用过授权的,可能获取不到以下三个参数
- // NSString *familyName = appleIDCredential.fullName.familyName;
- // NSString *givenName = appleIDCredential.fullName.givenName;
- // NSString *email = appleIDCredential.email;
- // NSLog(@"%@-%@-%@",familyName,givenName,email);
- // NSData *identityToken = appleIDCredential.identityToken;
- // NSData *authorizationCode = appleIDCredential.authorizationCode;
-
- // 服务器验证需要使用的参数
- // NSString *identityTokenStr = [[NSString alloc] initWithData:identityToken encoding:NSUTF8StringEncoding];
- // NSString *authorizationCodeStr = [[NSString alloc] initWithData:authorizationCode encoding:NSUTF8StringEncoding];
- // NSLog(@"%@\n\n%@", identityTokenStr, authorizationCodeStr);
-
- // Create an account in your system.
- // For the purpose of this demo app, store the userIdentifier in the keychain.
- // 需要使用钥匙串的方式保存用户的唯一信息
- [YostarKeychain save:KEYCHAIN_IDENTIFIER(@"userIdentifier") data:user];
-
- }else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]){
- // 这个获取的是iCloud记录的账号密码,需要输入框支持iOS 12 记录账号密码的新特性,如果不支持,可以忽略
- // Sign in using an existing iCloud Keychain credential.
- // 用户登录使用现有的密码凭证
- // ASPasswordCredential *passwordCredential = (ASPasswordCredential *)authorization.credential;
- // 密码凭证对象的用户标识 用户的唯一标识
- // NSString *user = passwordCredential.user;
- // // 密码凭证对象的密码
- // NSString *password = passwordCredential.password;
- // NSLog(@"%@-%@",user,password);
- }else{
- NSLog(@"授权信息均不符");
-
- }
- }
- // 授权失败的回调
- - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
- // 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;
- }
-
- NSLog(@"%@", errorMsg);
- }
- // 告诉代理应该在哪个window 展示内容给用户
- - (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
- NSLog(@"88888888888");
- // 返回window
- return [UIApplication sharedApplication].windows.lastObject;
- }
- @end
|