158 lines
5.1 KiB
Plaintext
158 lines
5.1 KiB
Plaintext
//
|
|
// UIViewController+Wallet.m
|
|
// Unity-iPhone
|
|
//
|
|
// Created by zhl on 2022/9/1.
|
|
//
|
|
|
|
#import "UIViewController+Wallet.h"
|
|
|
|
#import <TikTokOpenSDK/TikTokOpenSDKAuth.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"];
|
|
}
|
|
}
|
|
|
|
#pragma mark -- Sign In with TikTok
|
|
- (void) signWithTikTok:(NSString *) funid {
|
|
/* STEP 1: Create the request and set permissions */
|
|
NSArray *scopes = [NSArray arrayWithObjects: @"user.info.basic,video.list", nil]; // list your scopes;
|
|
NSOrderedSet *scopesSet = [NSOrderedSet orderedSetWithArray:scopes];
|
|
TikTokOpenSDKAuthRequest *request = [[TikTokOpenSDKAuthRequest alloc] init];
|
|
request.permissions = scopesSet;
|
|
|
|
/* STEP 2: Send the request */
|
|
__weak typeof(self) ws = self;
|
|
[request sendAuthRequestViewController:self
|
|
completion:^(TikTokOpenSDKAuthResponse *_Nonnull resp) {
|
|
__strong typeof(ws) sf = ws;
|
|
|
|
/* STEP 3: Parse and handle the response */
|
|
if (resp.errCode == 0) {
|
|
NSString *responseCode = resp.code;
|
|
// Upload response code to your server and obtain user access token
|
|
NSLog(@"tiktok resp: %@", responseCode);
|
|
} else {
|
|
// User authorization failed. Handle errors
|
|
NSLog(@"tiktok resp error: %@", resp.description);
|
|
}
|
|
}];
|
|
}
|
|
|
|
|
|
-(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
|