ios-unity/Classes_cocos/UIViewController+QR.mm
2022-12-07 16:02:13 +08:00

153 lines
6.2 KiB
Plaintext

//
// 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"
#import "SimpleQRViewController.h"
#import "LBXPermission.h"
#import "LBXPermissionSetting.h"
#include "QrCodeViewController.h"
#include <string>
#import "NSString+Customer.h"
#include "JcWallet.h"
#import "LBXScanNative.h"
#import "ToastManager.h"
@interface UIViewController (QR)
@end
static QRCodeReaderViewController *qrcodeReaderVC = nil;
static SimpleQRViewController *sqrVC = nil;
@implementation UIViewController (QR)
-(void)showRestoreQR:(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)showQRCode:(NSString *)content {
if (qrcodeReaderVC == nil) {
sqrVC = [SimpleQRViewController new];
}
sqrVC.content = content;
// next three code set transparent for simple qr view
self.definesPresentationContext = YES;
sqrVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
sqrVC.view.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 100.0/255.0];
[self presentViewController:sqrVC animated:YES completion:nil];
}
-(void)loadRestoreKey:(NSString *)funid oid:(NSString *) oid{
NSLog(@"loadRestoreKey::funid: %@, oid:%@", funid, oid);
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:UIAlertControllerStyleAlert];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler: ^(UIAlertAction *action){
[self nativeCb:funid hasErr:YES dataStr: @"user cancel"];
}];
UIAlertAction *scanAction = [UIAlertAction actionWithTitle:@"Scan"
style:UIAlertActionStyleDefault
handler: ^(UIAlertAction *action){
[self scanQRCode:funid title: @"" restore:YES ];
}];
[alertController addAction:actionCancel];
[alertController addAction:scanAction];
[self presentViewController:alertController animated:YES completion:nil];
});
}
-(void)beginScanQRCode:(NSString *)funid title:(NSString *) title{
std::string sfunid = std::string([funid UTF8String], [funid lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
__weak __typeof(self) weakSelf = self;
if ([QRCodeReader supportsMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]) {
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 (qrcodeReaderVC == nil) {
QRCodeReader *reader = [QRCodeReader readerWithMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
qrcodeReaderVC = [QRCodeReaderViewController readerWithCancelButtonTitle:@"Cancel" codeReader:reader startScanningAtLoad:YES showSwitchCameraButton:YES showTorchButton:YES];
qrcodeReaderVC.modalPresentationStyle = UIModalPresentationFormSheet;
}
[qrcodeReaderVC setCompletionWithBlock:^(NSString *resultAsString, NSError *error){
if (error != nil) {
[self nativeCb:funid hasErr:YES dataStr: error.localizedDescription];
} else {
NSLog(@"scan result: %@", resultAsString);
[self nativeCb:funid hasErr:NO dataStr: resultAsString];
}
}];
[self presentViewController:qrcodeReaderVC animated:YES completion:NULL];
});
}
}
-(void)scanQRCode:(NSString *)funid title:(NSString *) title{
[self scanQRCode:funid title:title restore:false];
}
-(void)scanQRCode:(NSString *)funid title:(NSString *) title restore: (BOOL) restore{
NSLog(@"scanQRCode:: funId: %@ title: %@", funid, title);
__weak __typeof(self) weakSelf = self;
[LBXPermission authorizeWithType:LBXPermissionType_Camera completion:^(BOOL granted, BOOL firstTime) {
if (granted) {
[weakSelf beginScanQRCode:funid title:title];
}
else if(!firstTime) {
[LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The camera is need to scan QR codes" cancel:@"Cancel" setting:@"Setting" completion:^{
if (restore) {
[weakSelf loadRestoreKey:funid oid:@""];
} else {
[weakSelf nativeCb:funid hasErr:YES dataStr: @"no permission, need resend"];
}
}];
}
}];
}
-(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";
NSString *paramStr;
if (hasErr) {
paramStr = [NSString stringWithFormat:@"{\"errcode\": 1, \"errmessage\": \"%@\"}", dataStr];
} else {
paramStr = [NSString stringWithFormat:@"{\"errcode\": 0, \"data\": \"%@\"}", dataStr];
}
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());
}
@end