130 lines
4.1 KiB
Plaintext
130 lines
4.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 "NSString+Customer.h"
|
|
#import "NSData+Base64.h"
|
|
#import "AppleSignIn.h"
|
|
|
|
@import GoogleSignIn;
|
|
|
|
static NSString * const kClientID =
|
|
@"53206975661-0d6q9pqljn84n9l63gm0to1ulap9cbk4.apps.googleusercontent.com";
|
|
|
|
|
|
@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 nativeCb:funid hasErr:YES dataStr: error.localizedDescription];
|
|
return;
|
|
}
|
|
[self refreshTokenID: user funid:funid];
|
|
}];
|
|
}
|
|
|
|
-(void) refreshTokenID:(GIDGoogleUser *)user funid:(NSString*) funid{
|
|
[user.authentication doWithFreshTokens:^(GIDAuthentication * _Nullable authentication,
|
|
NSError * _Nullable error) {
|
|
if (error) {
|
|
[self nativeCb:funid hasErr:YES dataStr: error.localizedDescription];
|
|
return;
|
|
}
|
|
if (authentication == nil) {
|
|
[self nativeCb:funid hasErr:YES dataStr: @"empty authenication"];
|
|
return;
|
|
}
|
|
|
|
NSString *idToken = authentication.idToken;
|
|
// Send ID token to backend (example below).
|
|
NSLog(@"idToken: %@", idToken);
|
|
[self nativeCb:funid hasErr:NO dataStr:idToken];
|
|
}];
|
|
}
|
|
|
|
-(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];
|
|
}
|
|
|
|
#pragma mark- - Sign In With Apple
|
|
- (void) signWithApple:(NSString *)funid{
|
|
if (@available(iOS 13.0, *)) {
|
|
[[AppleSignIn sharedInstance] signIn:funid presentingViewController:self completion:^(NSString *idToken, NSError *error){
|
|
if (error != nil) {
|
|
[self nativeCb:funid hasErr:YES dataStr: error.localizedDescription];
|
|
} else {
|
|
NSLog(@"signWithApple: %@", idToken);
|
|
[self nativeCb:funid hasErr:NO dataStr:idToken];
|
|
}
|
|
}];
|
|
} else {
|
|
NSLog(@"system is lower");
|
|
[self nativeCb:funid hasErr:YES dataStr: @"system is lower"];
|
|
}
|
|
}
|
|
|
|
|
|
-(void)nativeCb:(NSString *)funid hasErr: (BOOL) hasErr dataStr:(NSString *) dataStr {
|
|
if ([NSString isStringEmpty:funid]) {
|
|
NSLog(@"nativeCallBack with empty funid: %@", funid);
|
|
return;
|
|
}
|
|
std::string methodName = "nativeCallBack";
|
|
NSString *paramStr;
|
|
if (hasErr) {
|
|
paramStr = [NSString stringWithFormat:@"{\"errcode\": 1, \"errmessage\": \"%@\"}", dataStr];
|
|
} else {
|
|
paramStr = [NSString stringWithFormat:@"{\"errcode\": 0, \"data\": \"%@\"}", dataStr];
|
|
}
|
|
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());
|
|
}
|
|
|
|
@end
|