211 lines
9.2 KiB
Plaintext
211 lines
9.2 KiB
Plaintext
//
|
|
// UIViewController+Wallet.m
|
|
// Unity-iPhone
|
|
//
|
|
// Created by zhl on 2022/9/1.
|
|
//
|
|
|
|
#import "UIViewController+Wallet.h"
|
|
#import "QRCodeReaderViewController.h"
|
|
#import "QRCodeReader.h"
|
|
#import "QRCodeReaderDelegate.h"
|
|
#include <string>
|
|
#include "WalletEvent.h"
|
|
#include "JcWallet.h"
|
|
#import <GoogleSignIn/GoogleSignIn.h>
|
|
#include "SSKeychain.h"
|
|
@import GoogleSignIn;
|
|
|
|
static NSString * const kClientID =
|
|
@"53206975661-0d6q9pqljn84n9l63gm0to1ulap9cbk4.apps.googleusercontent.com";
|
|
static NSString * const cebgWalletService = @"CEBG-wallet";
|
|
|
|
@implementation UIViewController (Wallet)
|
|
|
|
+(void)toWallet:(NSString *)url{
|
|
UIApplication *app = [UIApplication sharedApplication];
|
|
[app openURL:[NSURL URLWithString:url]];
|
|
}
|
|
|
|
-(void)showQRCode:(NSString *) content title:(NSString *) title oid:(NSString *) oid {
|
|
NSLog(@"showQRCode content: %@, title: %@, oid: %@", content, title, oid);
|
|
}
|
|
-(void)loadRestoreKey:(NSString *)funid oid:(NSString *) oid{
|
|
NSLog(@"loadRestoreKey::funid: %@, oid:%@", funid, oid);
|
|
}
|
|
-(void)saveKey:(NSString *) account key:(NSString *) key {
|
|
NSLog(@"saveKey::account:%@, key: %@", account, key);
|
|
[SSKeychain setPassword:key forService:cebgWalletService account:account];
|
|
[SSKeychain setAccessibilityType:kSecAttrAccessibleWhenUnlocked];
|
|
}
|
|
-(NSString *)loadKey:(NSString *) account {
|
|
NSLog(@"loadKey::account:%@", account);
|
|
NSError *error = nil;
|
|
NSString *password = [SSKeychain passwordForService:cebgWalletService account:account error:&error];
|
|
if ([error code] == SSKeychainErrorNotFound) {
|
|
NSLog(@"password not found, account: %@", account);
|
|
return @"";
|
|
} else {
|
|
NSLog(@"password for %@ is %@", account, password);
|
|
return password;
|
|
}
|
|
}
|
|
|
|
-(void)scanQRCode:(NSString *)funid title:(NSString *) title{
|
|
NSLog(@"scanQRCode:: funId: %@ title: %@", funid, title);
|
|
std::string sfunid = std::string([funid UTF8String], [funid lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
|
|
if ([QRCodeReader supportsMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]) {
|
|
static QRCodeReaderViewController *vc = nil;
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
// if we are active again, we don't need to do this anymore
|
|
if (vc == nil) {
|
|
QRCodeReader *reader = [QRCodeReader readerWithMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
|
|
vc = [QRCodeReaderViewController readerWithCancelButtonTitle:@"Cancel" codeReader:reader startScanningAtLoad:YES showSwitchCameraButton:YES showTorchButton:YES];
|
|
vc.modalPresentationStyle = UIModalPresentationFormSheet;
|
|
}
|
|
|
|
[vc setCompletionWithBlock:^(NSString *resultAsString) {
|
|
NSLog(@"Completion with result: %@", resultAsString);
|
|
[self dismissViewControllerAnimated:YES completion:^{
|
|
|
|
NSString *result;
|
|
if (resultAsString.length > 0) {
|
|
result = [NSString stringWithFormat:@"{errcode: 0, data: '%@'}", resultAsString];
|
|
} else {
|
|
result = [NSString stringWithFormat:@"{errcode: 1, errmsg: 'cancel'}"];
|
|
}
|
|
std::string sresult = std::string([result UTF8String], [result lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
|
|
WalletEvent::Emit(sfunid.c_str(), sresult.c_str());
|
|
}];
|
|
}];
|
|
[self presentViewController:vc animated:YES completion:NULL];
|
|
});
|
|
}
|
|
else {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
|
|
message:@"The camera is need to scan QR codes"
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
//We add buttons to the alert controller by creating UIAlertActions:
|
|
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Cancel"
|
|
style:UIAlertActionStyleCancel
|
|
handler: ^(UIAlertAction *action){
|
|
NSLog(@"alert cancel pressed");
|
|
}]; //You can use a block here to handle a press on this button
|
|
//
|
|
UIAlertAction *setting = [UIAlertAction actionWithTitle:@"Setting"
|
|
style:UIAlertActionStyleDefault
|
|
handler: ^(UIAlertAction *action){
|
|
NSLog(@"setting pressed");
|
|
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
|
|
if ([[UIApplication sharedApplication] canOpenURL:url]){
|
|
[[UIApplication sharedApplication] openURL:url];
|
|
}
|
|
}];
|
|
[alertController addAction:actionOk];
|
|
[alertController addAction:setting];
|
|
[self presentViewController:alertController animated:YES completion:nil];
|
|
});
|
|
|
|
std::string sresult = "{errcode: 1, errmsg: 'no camera permission'}";
|
|
WalletEvent::Emit(sfunid.c_str(), sresult.c_str());
|
|
}
|
|
}
|
|
|
|
-(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);
|
|
}
|
|
|
|
-(void)checkOnSvr:(NSString *) idToken {
|
|
// NSString *signinEndpoint = @"https://yourbackend.example.com/tokensignin";
|
|
// NSDictionary *params = @{@"idtoken": idToken};
|
|
//
|
|
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:signinEndpoint];
|
|
// [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
|
|
// [request setHTTPMethod:@"POST"];
|
|
// [request setHTTPBody:[self httpBodyForParamsDictionary:params]];
|
|
//
|
|
// NSOperationQueue *queue = [[NSOperationQueue alloc] init];
|
|
// [NSURLConnection sendAsynchronousRequest:request
|
|
// queue:queue
|
|
// completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
|
|
// if (error) {
|
|
// NSLog(@"Error: %@", error.localizedDescription);
|
|
// } else {
|
|
// NSLog(@"Signed in as %@", data.bytes);
|
|
// }
|
|
// }];
|
|
}
|
|
|
|
|
|
@end
|