ios-unity/Classes_cocos/UIViewController+Wallet.mm
2023-08-03 13:14:23 +08:00

215 lines
7.5 KiB
Plaintext

//
// UIViewController+Wallet.m
// Unity-iPhone
//
// Created by zhl on 2022/9/1.
//
#import "UIViewController+Wallet.h"
#import "WebPageViewController.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 FBSDKLoginKit;
@import GoogleSignIn;
static NSString * const kClientID =
@"53206975661-0d6q9pqljn84n9l63gm0to1ulap9cbk4.apps.googleusercontent.com";
static WebPageViewController *webpageVC = nil;
@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
[self nativeCb:funid hasErr:NO dataStr:responseCode];
NSLog(@"tiktok resp: %@", responseCode);
} else {
// User authorization failed. Handle errors
NSLog(@"tiktok resp error: %@", resp.description);
[self nativeCb:funid hasErr:YES dataStr: resp.description];
}
}];
}
#pragma mark -- Sign In with Facebook
- (void) signWithFacebook:(NSString *) funid {
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
if ([FBSDKAccessToken currentAccessToken]) {
NSLog(@"Token is available, no need login again : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[self nativeCb:funid hasErr:NO dataStr: [[FBSDKAccessToken currentAccessToken]tokenString]];
} else {
[loginManager logInWithPermissions:@[@"public_profile", @"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
[self nativeCb:funid hasErr:YES dataStr: error.description];
} else if (result.isCancelled) {
[self nativeCb:funid hasErr:YES dataStr: @"user cancel"];
} else {
if ([FBSDKAccessToken currentAccessToken]) {
NSString *token = [[FBSDKAccessToken currentAccessToken]tokenString];
NSLog(@"Token is available : %@", token);
[self nativeCb:funid hasErr:NO dataStr: token];
}
else {
[self nativeCb:funid hasErr:YES dataStr: @"no token"];
}
}
}];
}
}
#pragma mark -- Sign In with Twitter
- (void) signWithTwitter:(NSString *) funid {
}
-(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";
NSMutableDictionary *json = [[NSMutableDictionary alloc] init];
if (hasErr) {
json[@"errcode"] = @1;
json[@"errmessage"] = dataStr;
} else {
json[@"errcode"] = @0;
json[@"data"] = dataStr;
}
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
std::string sfunid = std::string([funid UTF8String], [funid lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Got an error: %@", error);
NSString *errorStr = [NSString stringWithFormat:@"{\"errcode\": 1, \"errmessage\": \"%@\"}", error];
std::string sparam = std::string([errorStr UTF8String], [errorStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
cocos2d::nativeCallBack(sfunid.c_str(), methodName.c_str(), sparam.c_str());
return;
}
std::string sparam = std::string([jsonString UTF8String], [jsonString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
cocos2d::nativeCallBack(sfunid.c_str(), methodName.c_str(), sparam.c_str());
}
#pragma mark -- show webpage
-(void)showPage:(NSString *)url{
if (webpageVC == nil) {
webpageVC = [WebPageViewController new];
}
webpageVC.url = url;
self.definesPresentationContext = YES;
webpageVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:webpageVC animated:YES completion:nil];
}
@end