ios-unity/Classes_cocos/UIViewController+Wallet.mm
2022-09-07 19:43:44 +08:00

158 lines
6.6 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"
#import <GoogleSignIn/GoogleSignIn.h>
@import GoogleSignIn;
static NSString * const kClientID =
@"165555585193-alshovj5m91vlmj8uuhb11e6msvq32gm.apps.googleusercontent.com";
@implementation UIViewController (Wallet)
+(void)toWallet:(NSString *)url{
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString:url]];
}
-(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 {
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];
}];
}
-(void) refreshTokenID:(GIDGoogleUser *)user {
[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);
}];
}
-(void)signWithGoogle:(NSString *)funid {
[GIDSignIn.sharedInstance restorePreviousSignInWithCompletion:^(GIDGoogleUser * _Nullable user,
NSError * _Nullable error) {
if (error) {
// Show the app's signed-out state.
[self signToGoogle];
} else {
// Show the app's signed-in state.
[self refreshTokenID: user];
}
}];
}
-(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);
}
@end