149 lines
7.5 KiB
Objective-C
149 lines
7.5 KiB
Objective-C
#import "SignWithApple.h"
|
||
|
||
static AppleAuth * _appleAuth;
|
||
|
||
@implementation AppleAuth
|
||
+(AppleAuth *)instance{
|
||
if(_appleAuth==nil){
|
||
_appleAuth = [[AppleAuth alloc] init];
|
||
}
|
||
return _appleAuth;
|
||
}
|
||
#pragma mark- 授权苹果ID
|
||
- (void)authorizationAppleID{
|
||
NSLog(@"cypen");
|
||
if (@available(iOS 13.0, *)) {
|
||
// 基于用户的Apple ID授权用户,生成用户授权请求的一种机制
|
||
ASAuthorizationAppleIDProvider * appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
|
||
// 创建新的AppleID 授权请求
|
||
ASAuthorizationAppleIDRequest * authAppleIDRequest = [appleIDProvider createRequest];
|
||
// 在用户授权期间请求的联系信息
|
||
// authAppleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
|
||
//如果 KeyChain 里面也有登录信息的话,可以直接使用里面保存的用户名和密码进行登录。
|
||
// ASAuthorizationPasswordRequest * passwordRequest = [[[ASAuthorizationPasswordProvider alloc] init] createRequest];
|
||
|
||
NSMutableArray <ASAuthorizationRequest *> * array = [NSMutableArray arrayWithCapacity:2];
|
||
if (authAppleIDRequest) {
|
||
[array addObject:authAppleIDRequest];
|
||
}
|
||
// if (passwordRequest) {
|
||
// [array addObject:passwordRequest];
|
||
// }
|
||
NSArray <ASAuthorizationRequest *> * requests = [array copy];
|
||
// 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
|
||
ASAuthorizationController * authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:requests];
|
||
// 设置授权控制器通知授权请求的成功与失败的代理
|
||
authorizationController.delegate = self;
|
||
// 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
|
||
authorizationController.presentationContextProvider = self;
|
||
// 在控制器初始化期间启动授权流
|
||
[authorizationController performRequests];
|
||
}
|
||
}
|
||
- (void) authorizationGoogleID{
|
||
NSLog(@"cypgoogle");
|
||
GIDConfiguration *config = [[GIDConfiguration alloc] initWithClientID:[FIRApp defaultApp].options.clientID];
|
||
__weak __auto_type weakSelf = self;
|
||
UnityAppController * root = (UnityAppController*)[UIApplication sharedApplication].delegate;
|
||
[GIDSignIn.sharedInstance signInWithConfiguration:config presentingViewController:root.rootViewController callback:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {
|
||
__auto_type strongSelf = weakSelf;
|
||
if (strongSelf == nil) { return; }
|
||
|
||
if (error == nil) {
|
||
GIDAuthentication *authentication = user.authentication;
|
||
UnitySendMessage("LoginPanel", "OnMobileGoogleLoginResp", [NSString stringWithFormat:@"true;%@|%@",user.userID,authentication.idToken ].UTF8String);
|
||
// FIRAuthCredential *credential =
|
||
// [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
|
||
// accessToken:authentication.accessToken];
|
||
// ...
|
||
} else {
|
||
// ...
|
||
}
|
||
}];
|
||
}
|
||
#pragma mark- ASAuthorizationControllerDelegate
|
||
// 授权成功
|
||
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
|
||
NSLog(@"cypcg");
|
||
if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
|
||
|
||
ASAuthorizationAppleIDCredential * credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
|
||
|
||
// 苹果用户唯一标识符,该值在同一个开发者账号下的所有 App 下是一样的,开发者可以用该唯一标识符与自己后台系统的账号体系绑定起来。
|
||
NSString * userID = credential.user;
|
||
NSString * token = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
|
||
|
||
// NSString * test = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding];
|
||
// NSLog(test);
|
||
UnitySendMessage("LoginPanel", "OnAppleResp", [NSString stringWithFormat:@"true;%@,%@",userID,token ].UTF8String);
|
||
//把用户的唯一标识 传给后台 判断该用户是否绑定手机号,如果绑定了直接登录,如果没绑定跳绑定手机号页面
|
||
// // 苹果用户信息 如果授权过,可能无法再次获取该信息
|
||
// NSPersonNameComponents * fullName = credential.fullName;
|
||
// NSString * email = credential.email;
|
||
//
|
||
// // 服务器验证需要使用的参数
|
||
// NSString * authorizationCode = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding];
|
||
// NSString * identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
|
||
//
|
||
// // 用于判断当前登录的苹果账号是否是一个真实用户,取值有:unsupported、unknown、likelyReal
|
||
// ASUserDetectionStatus realUserStatus = credential.realUserStatus;
|
||
|
||
// NSLog(@"userID: %@", userID);
|
||
// NSLog(@"fullName: %@", fullName);
|
||
// NSLog(@"email: %@", email);
|
||
// NSLog(@"authorizationCode: %@", authorizationCode);
|
||
// NSLog(@"identityToken: %@", identityToken);
|
||
// NSLog(@"realUserStatus: %@", @(realUserStatus));
|
||
}else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]) {
|
||
// 这个获取的是iCloud记录的账号密码,需要输入框支持iOS 12 记录账号密码的新特性,如果不支持,可以忽略
|
||
// 用户登录使用现有的密码凭证
|
||
// ASPasswordCredential * passwordCredential = (ASPasswordCredential *)authorization.credential;
|
||
// 密码凭证对象的用户标识 用户的唯一标识
|
||
// NSString * user = passwordCredential.user;
|
||
// NSLog(user);
|
||
// UnitySendMessage("LoginPanel", "OnAppleResp", [NSString stringWithFormat:@"true,%@",user ].UTF8String);
|
||
//把用户的唯一标识 传给后台 判断该用户是否绑定手机号,如果绑定了直接登录,如果没绑定跳绑定手机号页面
|
||
|
||
// // 密码凭证对象的密码
|
||
// NSString * password = passwordCredential.password;
|
||
// NSLog(@"userID: %@", user);
|
||
// NSLog(@"password: %@", password);
|
||
|
||
} else {
|
||
|
||
}
|
||
}
|
||
// 授权失败
|
||
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
|
||
NSString *errorMsg = nil;
|
||
NSLog(@"cypsb");
|
||
switch (error.code) {
|
||
case ASAuthorizationErrorCanceled:
|
||
errorMsg = @"用户取消了授权请求";
|
||
break;
|
||
case ASAuthorizationErrorFailed:
|
||
errorMsg = @"授权请求失败";
|
||
break;
|
||
case ASAuthorizationErrorInvalidResponse:
|
||
errorMsg = @"授权请求响应无效";
|
||
break;
|
||
case ASAuthorizationErrorNotHandled:
|
||
errorMsg = @"未能处理授权请求";
|
||
break;
|
||
case ASAuthorizationErrorUnknown:
|
||
errorMsg = @"授权请求失败未知原因";
|
||
break;
|
||
}
|
||
UnitySendMessage("LoginPanel", "OnAppleResp", [NSString stringWithFormat:@"false;%@",errorMsg ].UTF8String);
|
||
}
|
||
@end
|
||
|
||
void authorizationAppleID(){
|
||
AppleAuth * a = [AppleAuth instance];
|
||
[a authorizationAppleID];
|
||
}
|
||
|
||
void authorizationGoogleID(){
|
||
AppleAuth * a = [AppleAuth instance];
|
||
[a authorizationGoogleID];
|
||
} |