197 lines
7.1 KiB
Plaintext
197 lines
7.1 KiB
Plaintext
//
|
|
// UIViewController+Wallet.m
|
|
// Unity-iPhone
|
|
//
|
|
// Created by zhl on 2022/9/1.
|
|
//
|
|
|
|
#import "UIViewController+Wallet.h"
|
|
|
|
#include <string>
|
|
#include "WalletEvent.h"
|
|
#include "JcWallet.h"
|
|
#import <GoogleSignIn/GoogleSignIn.h>
|
|
#include "KeyChain/DataManager.h"
|
|
#import <AuthenticationServices/AuthenticationServices.h>
|
|
|
|
@import GoogleSignIn;
|
|
|
|
static NSString * const kClientID =
|
|
@"53206975661-0d6q9pqljn84n9l63gm0to1ulap9cbk4.apps.googleusercontent.com";
|
|
|
|
@interface UIViewController (Wallet)<ASAuthorizationControllerDelegate>
|
|
@end
|
|
|
|
@implementation UIViewController (Wallet)
|
|
|
|
+(void)toWallet:(NSString *)url{
|
|
UIApplication *app = [UIApplication sharedApplication];
|
|
[app openURL:[NSURL URLWithString:url]];
|
|
}
|
|
|
|
|
|
|
|
// save key to key chain
|
|
-(void)saveKey:(NSString *) account key:(NSString *) key {
|
|
[[DataManager sharedInstanceWith: SynLock] saveKey: account key: key];
|
|
}
|
|
|
|
// load key from key chain
|
|
-(NSString *)loadKey:(NSString *) account {
|
|
NSLog(@"loadKey::account:%@", account);
|
|
return [[DataManager sharedInstanceWith: SynLock] loadKey: account];
|
|
}
|
|
|
|
|
|
|
|
-(void)signToGoogle:(NSString *) funid {
|
|
GIDConfiguration *_configuration = [[GIDConfiguration alloc] initWithClientID:kClientID];
|
|
[GIDSignIn.sharedInstance signInWithConfiguration:_configuration
|
|
presentingViewController:self
|
|
completion:^(GIDGoogleUser *user, NSError *error) {
|
|
if (error) {
|
|
NSLog(@"Status: Authentication error: %@", error);
|
|
}
|
|
[self refreshUserInfo];
|
|
[self refreshTokenID: user funid:funid];
|
|
}];
|
|
}
|
|
-(void) refreshTokenID:(GIDGoogleUser *)user funid:(NSString*) funid{
|
|
[user.authentication doWithFreshTokens:^(GIDAuthentication * _Nullable authentication,
|
|
NSError * _Nullable error) {
|
|
if (error) { return; }
|
|
if (authentication == nil) { return; }
|
|
|
|
NSString *idToken = authentication.idToken;
|
|
// Send ID token to backend (example below).
|
|
NSLog(@"idToken: %@", idToken);
|
|
std::string methodName = "nativeCallBack";
|
|
NSString *paramStr = [NSString stringWithFormat:@"{\"errcode\": 0, \"data\": \"%@\"}", idToken];
|
|
std::string sfunid = std::string([funid UTF8String], [funid lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
|
|
std::string sparam = std::string([paramStr UTF8String], [paramStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
|
|
cocos2d::nativeCallBack(sfunid.c_str(), methodName.c_str(), sparam.c_str());
|
|
}];
|
|
}
|
|
|
|
-(void)signWithGoogle:(NSString *)funid {
|
|
[GIDSignIn.sharedInstance restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user,
|
|
NSError * _Nullable error) {
|
|
if (error) {
|
|
// Show the app's signed-out state.
|
|
[self signToGoogle: funid];
|
|
} else {
|
|
// Show the app's signed-in state.
|
|
[self refreshTokenID: user funid:funid];
|
|
}
|
|
}];
|
|
|
|
}
|
|
|
|
-(void)signOutGoogle:(NSString *)funid {
|
|
[GIDSignIn.sharedInstance signOut];
|
|
}
|
|
|
|
- (void)reportAuthStatus {
|
|
GIDGoogleUser *googleUser = [GIDSignIn.sharedInstance currentUser];
|
|
if (googleUser.authentication) {
|
|
NSLog(@"Status: Authenticated");
|
|
} else {
|
|
// To authenticate, use Google Sign-In button.
|
|
NSLog(@"Status: Not authenticated");
|
|
}
|
|
|
|
[self refreshUserInfo];
|
|
}
|
|
|
|
- (void)refreshUserInfo {
|
|
if (GIDSignIn.sharedInstance.currentUser.authentication == nil) {
|
|
return;
|
|
}
|
|
NSLog(@"email: %@", GIDSignIn.sharedInstance.currentUser.profile.email);
|
|
NSLog(@"username: %@", GIDSignIn.sharedInstance.currentUser.profile.name);
|
|
NSLog(@"userid: %@", GIDSignIn.sharedInstance.currentUser.userID);
|
|
NSLog(@"givenName: %@", GIDSignIn.sharedInstance.currentUser.profile.givenName);
|
|
NSLog(@"familyName: %@", GIDSignIn.sharedInstance.currentUser.profile.familyName);
|
|
NSLog(@"hostedDomain: %@", GIDSignIn.sharedInstance.currentUser.hostedDomain);
|
|
}
|
|
|
|
#pragma mark- - Sign In With Apple
|
|
- (void) signWithApple {
|
|
if (@available(iOS 13.0, *)) {
|
|
ASAuthorizationAppleIDProvider *appleIdProvider = [[ASAuthorizationAppleIDProvider alloc] init];
|
|
ASAuthorizationAppleIDRequest *request = appleIdProvider.createRequest;
|
|
request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
|
|
ASAuthorizationController *controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
|
|
controller.delegate = self;
|
|
[controller performRequests];
|
|
} else {
|
|
NSLog(@"system is lower");
|
|
}
|
|
}
|
|
|
|
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
|
|
if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
|
|
// 用户登录使用ASAuthorizationAppleIDCredential
|
|
ASAuthorizationAppleIDCredential *credential = authorization.credential;
|
|
NSString *user = credential.user;
|
|
NSData *identityToken = credential.identityToken;
|
|
NSLog(@"fullName - %@",credential.fullName);
|
|
//授权成功后,你可以拿到苹果返回的全部数据,根据需要和后台交互。
|
|
NSLog(@"user - %@ %@",user,identityToken);
|
|
//保存apple返回的唯一标识符
|
|
[[NSUserDefaults standardUserDefaults] setObject:user forKey:@"userIdentifier"];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
} else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]) {
|
|
// 用户登录使用现有的密码凭证
|
|
ASPasswordCredential *psdCredential = authorization.credential;
|
|
// 密码凭证对象的用户标识 用户的唯一标识
|
|
NSString *user = psdCredential.user;
|
|
NSString *psd = psdCredential.password;
|
|
NSLog(@"psduser - %@ %@",psd,user);
|
|
} else {
|
|
NSLog(@"授权信息不符");
|
|
}
|
|
}
|
|
|
|
#pragma mark - 授权回调失败
|
|
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
|
|
|
|
NSLog(@"错误信息:%@", 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
|