132 lines
4.2 KiB
Objective-C
132 lines
4.2 KiB
Objective-C
//
|
|
// AppleSignIn.m
|
|
// Unity-iPhone
|
|
//
|
|
// Created by zhl on 2022/11/28.
|
|
//
|
|
|
|
#import "AppleSignIn.h"
|
|
|
|
|
|
static id _instance;
|
|
|
|
|
|
@implementation AppleSignIn
|
|
|
|
+ (instancetype)sharedInstance{
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
_instance = [[self alloc] init];
|
|
});
|
|
return _instance;
|
|
}
|
|
|
|
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
_instance = [super allocWithZone:zone];
|
|
});
|
|
return _instance;
|
|
}
|
|
|
|
- (id)copyWithZone:(nullable NSZone *)zone {
|
|
return _instance;
|
|
}
|
|
|
|
- (void)signIn:(NSString *)funid presentingViewController:(UIViewController *)presentingViewController completion:(nullable AppleSignInCompletion)completion API_AVAILABLE(ios(13.0)){
|
|
self.completion = completion;
|
|
self.funid = funid;
|
|
ASAuthorizationAppleIDProvider *appleIdProvider = [[ASAuthorizationAppleIDProvider alloc] init];
|
|
ASAuthorizationAppleIDRequest *request = appleIdProvider.createRequest;
|
|
request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
|
|
ASAuthorizationController *controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
|
|
controller.delegate = self;
|
|
[controller performRequests];
|
|
}
|
|
|
|
#pragma mark -- ASAuthorizationControllerDelegate
|
|
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
|
|
if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
|
|
ASAuthorizationAppleIDCredential *credential = authorization.credential;
|
|
NSString *idToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
|
|
NSLog(@"identityToken: %@",idToken);
|
|
if (self.completion) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
self.completion(idToken, nil);
|
|
});
|
|
}
|
|
// NSString *user = credential.user;
|
|
// NSString *authStr = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding];
|
|
// NSLog(@"fullName: %@",credential.fullName);
|
|
// NSLog(@"user: %@",user);
|
|
|
|
// [self nativeCb:funid hasErr:NO dataStr:idToken];
|
|
}
|
|
// else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]) {
|
|
// // 用户登录使用现有的密码凭证
|
|
// ASPasswordCredential *psdCredential = authorization.credential;
|
|
// // 密码凭证对象的用户标识 用户的唯一标识
|
|
// NSString *user = psdCredential.user;
|
|
// NSString *psd = psdCredential.password;
|
|
// NSLog(@"psduser - %@ %@",psd,user);
|
|
// }
|
|
else {
|
|
NSError *err = [NSError errorWithDomain:@"apple_sign"
|
|
code:100
|
|
userInfo:@{
|
|
NSLocalizedDescriptionKey:@"authorization info mismath"
|
|
}];
|
|
[self throwError:err];
|
|
|
|
}
|
|
}
|
|
|
|
- (void) throwError:(NSError *) err {
|
|
if (self.completion) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
self.completion(nil, err);
|
|
});
|
|
}
|
|
}
|
|
|
|
#pragma mark -- ASAuthorizationControllerDelegate
|
|
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
|
|
NSLog(@"错误信息:%@", error);
|
|
[self throwError:error];
|
|
// NSString *errorMsg;
|
|
// switch (error.code) {
|
|
// case ASAuthorizationErrorCanceled:
|
|
// errorMsg = @"用户取消了授权请求";
|
|
// NSLog(@"errorMsg - %@",errorMsg);
|
|
// break;
|
|
//
|
|
// case ASAuthorizationErrorFailed:
|
|
// errorMsg = @"授权请求失败";
|
|
// NSLog(@"errorMsg - %@",errorMsg);
|
|
// break;
|
|
//
|
|
// case ASAuthorizationErrorInvalidResponse:
|
|
// errorMsg = @"授权请求响应无效";
|
|
// NSLog(@"errorMsg - %@",errorMsg);
|
|
// break;
|
|
//
|
|
// case ASAuthorizationErrorNotHandled:
|
|
// errorMsg = @"未能处理授权请求";
|
|
// NSLog(@"errorMsg - %@",errorMsg);
|
|
// break;
|
|
//
|
|
// case ASAuthorizationErrorUnknown:
|
|
// errorMsg = @"授权请求失败未知原因";
|
|
// NSLog(@"errorMsg - %@",errorMsg);
|
|
// break;
|
|
//
|
|
// default:
|
|
// break;
|
|
// }
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|