// // 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 #include "WalletEvent.h" @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()); } } @end