// // UIViewController+QR.m // Unity-iPhone // // Created by zhl on 2022/11/28. // #import "UIViewController+QR.h" #import "QRCodeReaderViewController.h" #import "QRCodeReader.h" #import "QRCodeReaderDelegate.h" #include "permission/LBXPermission.h" #include "permission/LBXPermissionSetting.h" #include "LBXScanNative.h" #include "LBXScanTypes.h" #include "QrCodeViewController.h" #include @interface UIViewController (QR) @end @implementation UIViewController (QR) -(void)showQRCode:(NSString *) content title:(NSString *) title oid:(NSString *) oid { NSLog(@"showQRCode content: %@, title: %@, oid: %@", content, title, oid); dispatch_async(dispatch_get_main_queue(), ^{ QrCodeViewController *vc = [QrCodeViewController new]; vc.content = content; vc.tipTitle = title; vc.autosave = TRUE; NSString *tipTxt = @"This QR code is only used to export account recovery private key.\n \ \n \ 1. Use CEBG client to scan the QR code to get the account recovery private key. \n\ \n \ 2. Account of ios cannot login to Andorid device. \n \ \n \ 3. Please keep your private key as safe as possible."; vc.tipTxt = tipTxt; [self presentViewController:vc animated:YES completion:nil]; }); } -(void)loadRestoreKey:(NSString *)funid oid:(NSString *) oid{ NSLog(@"loadRestoreKey::funid: %@, oid:%@", funid, oid); // if ([NSThread isMainThread]) { // // } dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Tips" message:@"In order to restore recovery key, please Scan QRCode or Pick from Photo Library." preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler: ^(UIAlertAction *action){ NSLog(@"alert cancel pressed"); }]; UIAlertAction *scanAction = [UIAlertAction actionWithTitle:@"Scan" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){ NSLog(@"scan pressed"); [self scanQRCode:funid title: @"" ]; }]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"Photo" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){ NSLog(@"photo pressed"); [self openLocalPhotoAlbum]; }]; [alertController addAction:actionCancel]; [alertController addAction:scanAction]; [alertController addAction:photoAction]; [self presentViewController:alertController animated:YES completion:nil]; }); } -(void)startScanQRCode:(NSString *)funid title:(NSString *) 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]; }); } } -(void)scanQRCode:(NSString *)funid title:(NSString *) title{ NSLog(@"scanQRCode:: funId: %@ title: %@", funid, title); // [self openLocalPhotoAlbum]; // [self signWithApple]; __weak __typeof(self) weakSelf = self; [LBXPermission authorizeWithType:LBXPermissionType_Camera completion:^(BOOL granted, BOOL firstTime) { if (granted) { [weakSelf startScanQRCode:funid title:title]; } else if(!firstTime) { [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The camera is need to scan QR codes" cancel:@"Cancel" setting:@"Setting" ]; } }]; } #pragma mark- - PhotoAlbum - (void)openLocalPhotoAlbum { __weak __typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ [LBXPermission authorizeWithType:LBXPermissionType_Photos completion:^(BOOL granted, BOOL firstTime) { if (granted) { [weakSelf openLocalPhoto]; } else if (!firstTime ) { [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The Photo Library is need to restore recovery key." cancel:@"Cancel" setting:@"Setting"]; } }]; }); } /*! * open local Photo Library */ - (void)openLocalPhoto { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; // crash on some mobile picker.allowsEditing = NO; [self presentViewController:picker animated:YES completion:nil]; } #pragma mark- - UIImagePickerControllerDelegate -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{ [self handPhotoDidFinishPickingMediaWithInfo:info]; }]; } - (void)handPhotoDidFinishPickingMediaWithInfo:(NSDictionary *)info { __block UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage]; if (!image){ image = [info objectForKey:UIImagePickerControllerOriginalImage]; } if (@available(iOS 8.0, *)) { __weak __typeof(self) weakSelf = self; [LBXScanNative recognizeImage:image success:^(NSArray *array) { [weakSelf scanResultWithArray:array]; }]; }else{ NSLog(@"native低于ios8.0不支持识别图片"); } } - (void)scanResultWithArray:(NSArray*)array { if (!array || array.count < 1) { NSLog(@"error scan photo"); return; } for (LBXScanResult *result in array) { NSLog(@"scanResult:%@",result.strScanned); } if (!array[0].strScanned || [array[0].strScanned isEqualToString:@""] ) { NSLog(@"识别失败了"); return; } LBXScanResult *scanResult = array[0]; } @end