diff --git a/Classes_cocos/NSString+Customer.h b/Classes_cocos/NSString+Customer.h new file mode 100644 index 0000000..5dddf2a --- /dev/null +++ b/Classes_cocos/NSString+Customer.h @@ -0,0 +1,16 @@ +// +// NSString+Customer.h +// Unity-iPhone +// +// Created by zhl on 2022/11/28. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface NSString (Customer) ++ (BOOL)isStringEmpty:(NSString *)string; +@end + +NS_ASSUME_NONNULL_END diff --git a/Classes_cocos/NSString+Customer.m b/Classes_cocos/NSString+Customer.m new file mode 100644 index 0000000..3d589ea --- /dev/null +++ b/Classes_cocos/NSString+Customer.m @@ -0,0 +1,27 @@ +// +// NSString+Customer.m +// Unity-iPhone +// +// Created by zhl on 2022/11/28. +// + +#import "NSString+Customer.h" + +@implementation NSString (Customer) ++ (BOOL)isStringEmpty:(NSString *)string { + if (((NSNull *) string == [NSNull null]) || (string == nil) ) { + return YES; + } + if (string == nil) { + return YES; + } else if ([string length] == 0) { + return YES; + } + string = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + if ([string isEqualToString:@""]) { + return YES; + } + return NO; +} +@end diff --git a/Classes_cocos/UIViewController+QR.h b/Classes_cocos/UIViewController+QR.h index 4603f9d..9ac66fe 100644 --- a/Classes_cocos/UIViewController+QR.h +++ b/Classes_cocos/UIViewController+QR.h @@ -10,9 +10,9 @@ NS_ASSUME_NONNULL_BEGIN @interface UIViewController (QR) --(void)scanQRCode:(NSString *)funid title:(NSString *) title; -(void)showQRCode:(NSString *) content title:(NSString *) title oid:(NSString *) oid; -(void)loadRestoreKey:(NSString *)funid oid:(NSString *) oid; +-(void)scanQRCode:(NSString *)funid title:(NSString *) title; @end NS_ASSUME_NONNULL_END diff --git a/Classes_cocos/UIViewController+QR.mm b/Classes_cocos/UIViewController+QR.mm index bff4bb0..7cfca2c 100644 --- a/Classes_cocos/UIViewController+QR.mm +++ b/Classes_cocos/UIViewController+QR.mm @@ -15,14 +15,14 @@ #include "LBXScanTypes.h" #include "QrCodeViewController.h" #include +#import "NSString+Customer.h" +#include "JcWallet.h" @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(), ^{ @@ -44,9 +44,6 @@ -(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." @@ -54,22 +51,19 @@ UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler: ^(UIAlertAction *action){ - NSLog(@"alert cancel pressed"); + [self nativeCb:funid hasErr:YES dataStr: @"user cancel"]; }]; UIAlertAction *scanAction = [UIAlertAction actionWithTitle:@"Scan" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){ - NSLog(@"scan pressed"); - [self scanQRCode:funid title: @"" ]; + [self scanQRCode:funid title: @"" restore:YES ]; }]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"Photo" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){ - NSLog(@"photo pressed"); - [self openLocalPhotoAlbum]; - + [self openLocalPhotoAlbum: funid]; }]; [alertController addAction:actionCancel]; [alertController addAction:scanAction]; @@ -78,8 +72,9 @@ }); } --(void)startScanQRCode:(NSString *)funid title:(NSString *) title{ +-(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 QRCodeReaderViewController *vc = nil; static dispatch_once_t onceToken; @@ -98,12 +93,11 @@ NSString *result; if (resultAsString.length > 0) { - result = [NSString stringWithFormat:@"{errcode: 0, data: '%@'}", resultAsString]; + [self nativeCb:funid hasErr:NO dataStr: resultAsString]; } else { - result = [NSString stringWithFormat:@"{errcode: 1, errmsg: 'cancel'}"]; + NSLog(@"user cancel scan"); + [weakSelf loadRestoreKey:funid oid:@""]; } - 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]; @@ -112,35 +106,42 @@ } -(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); - // [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]; + [weakSelf beginScanQRCode:funid title:title]; } - else if(!firstTime) - { - [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The camera is need to scan QR codes" cancel:@"Cancel" setting:@"Setting" ]; + 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"]; + } + }]; } }]; } #pragma mark- - PhotoAlbum -- (void)openLocalPhotoAlbum { +- (void)openLocalPhotoAlbum:(NSString *)funid { __weak __typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ [LBXPermission authorizeWithType:LBXPermissionType_Photos completion:^(BOOL granted, BOOL firstTime) { if (granted) { - [weakSelf openLocalPhoto]; + [weakSelf openLocalPhoto:funid]; } else if (!firstTime ) { - [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The Photo Library is need to restore recovery key." cancel:@"Cancel" setting:@"Setting"]; + [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"Error" msg:@"The Photo Library is need to restore recovery key." cancel:@"Cancel" setting:@"Setting" completion:^{ + [weakSelf loadRestoreKey:funid oid:@""]; + }]; } }]; }); @@ -150,30 +151,31 @@ /*! * open local Photo Library */ -- (void)openLocalPhoto -{ +- (void)openLocalPhoto:(NSString *)funid { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; - picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; - picker.delegate = self; - // crash on some mobile picker.allowsEditing = NO; - + picker.title = funid; [self presentViewController:picker animated:YES completion:nil]; } #pragma mark- - UIImagePickerControllerDelegate --(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info -{ +-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [picker dismissViewControllerAnimated:YES completion:^{ - - [self handPhotoDidFinishPickingMediaWithInfo:info]; + NSString *funid = picker.title; + [self handPhotoDidFinishPickingMediaWithInfo:funid info:info]; }]; } -- (void)handPhotoDidFinishPickingMediaWithInfo:(NSDictionary *)info -{ +-(void)imagePickerControllerDidCancel:(UIImagePickerController*)picker { + [picker dismissViewControllerAnimated:YES completion:^{ + NSLog(@"imagePickerControllerDidCancel %@", picker.title); + [self loadRestoreKey: picker.title oid: @"" ]; + }]; +} + +- (void)handPhotoDidFinishPickingMediaWithInfo:(NSString *)funid info:(NSDictionary *)info { __block UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage]; if (!image){ @@ -182,31 +184,41 @@ if (@available(iOS 8.0, *)) { __weak __typeof(self) weakSelf = self; [LBXScanNative recognizeImage:image success:^(NSArray *array) { - [weakSelf scanResultWithArray:array]; + [weakSelf scanResultWithArray:funid array:array]; }]; }else{ - NSLog(@"native低于ios8.0不支持识别图片"); + [self nativeCb:funid hasErr:YES dataStr: @"os version is lower then IOS8.0"]; } } -- (void)scanResultWithArray:(NSArray*)array -{ - if (!array || array.count < 1) - { - NSLog(@"error scan photo"); +- (void)scanResultWithArray:(NSString *)funid array:(NSArray*)array { + if (!array || array.count < 1) { + [self nativeCb:funid hasErr:YES dataStr: @"error scan photo"]; return; } - - for (LBXScanResult *result in array) { - NSLog(@"scanResult:%@",result.strScanned); - } - if (!array[0].strScanned || [array[0].strScanned isEqualToString:@""] ) { - NSLog(@"识别失败了"); + [self nativeCb:funid hasErr:YES dataStr: @"failed decoding QR code"]; return; } LBXScanResult *scanResult = array[0]; - + [self nativeCb:funid hasErr:NO dataStr: scanResult.strScanned]; +} + +-(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 diff --git a/Classes_cocos/UIViewController+Wallet.h b/Classes_cocos/UIViewController+Wallet.h index 23665d6..b61ce4a 100644 --- a/Classes_cocos/UIViewController+Wallet.h +++ b/Classes_cocos/UIViewController+Wallet.h @@ -17,4 +17,5 @@ -(void)signOutGoogle:(NSString *)funid; -(void)saveKey:(NSString *) account key:(NSString *) key; -(NSString *)loadKey:(NSString *) account; +//-(void)nativeCb:(NSString *)funid hasErr: (BOOL) hasErr dataStr:(NSString *) dataStr; @end diff --git a/Classes_cocos/UIViewController+Wallet.mm b/Classes_cocos/UIViewController+Wallet.mm index 83963f3..bc861de 100644 --- a/Classes_cocos/UIViewController+Wallet.mm +++ b/Classes_cocos/UIViewController+Wallet.mm @@ -12,6 +12,7 @@ #include "JcWallet.h" #import #include "KeyChain/DataManager.h" +#import "NSString+Customer.h" #import @import GoogleSignIn; @@ -29,8 +30,6 @@ static NSString * const kClientID = [app openURL:[NSURL URLWithString:url]]; } - - // save key to key chain -(void)saveKey:(NSString *) account key:(NSString *) key { [[DataManager sharedInstanceWith: SynLock] saveKey: account key: key]; @@ -42,8 +41,6 @@ static NSString * const kClientID = return [[DataManager sharedInstanceWith: SynLock] loadKey: account]; } - - -(void)signToGoogle:(NSString *) funid { GIDConfiguration *_configuration = [[GIDConfiguration alloc] initWithClientID:kClientID]; [GIDSignIn.sharedInstance signInWithConfiguration:_configuration @@ -56,6 +53,7 @@ static NSString * const kClientID = [self refreshTokenID: user funid:funid]; }]; } + -(void) refreshTokenID:(GIDGoogleUser *)user funid:(NSString*) funid{ [user.authentication doWithFreshTokens:^(GIDAuthentication * _Nullable authentication, NSError * _Nullable error) { @@ -65,11 +63,7 @@ static NSString * const kClientID = 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()); + [self nativeCb:funid hasErr:NO dataStr:idToken]; }]; } @@ -155,7 +149,6 @@ static NSString * const kClientID = #pragma mark - 授权回调失败 - (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){ - NSLog(@"错误信息:%@", error); NSString *errorMsg; switch (error.code) { @@ -189,8 +182,21 @@ static NSString * const kClientID = } } - - - +-(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 diff --git a/Unity-iPhone.xcodeproj/project.pbxproj b/Unity-iPhone.xcodeproj/project.pbxproj index 184a14c..1d8f2b0 100644 --- a/Unity-iPhone.xcodeproj/project.pbxproj +++ b/Unity-iPhone.xcodeproj/project.pbxproj @@ -79,6 +79,7 @@ D0DD4D8D8AC82F06A4331428 /* libil2cpp.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 924940699744FB37428516DD /* libil2cpp.a */; }; D52A8DA1288E6547006574E8 /* libuv_a.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D52A8D9F288E6547006574E8 /* libuv_a.a */; }; D5538BA5287E9908000BDFB6 /* WalletEvent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D5538BA3287E9908000BDFB6 /* WalletEvent.cpp */; }; + D5608AF929348B83007F146A /* NSString+Customer.m in Sources */ = {isa = PBXBuildFile; fileRef = D5608AF829348B83007F146A /* NSString+Customer.m */; }; D56436422930AAF700E2B633 /* UIView+Toast.m in Sources */ = {isa = PBXBuildFile; fileRef = D56436412930AAF700E2B633 /* UIView+Toast.m */; }; D56436462930ABAB00E2B633 /* UIUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = D56436452930ABAB00E2B633 /* UIUtils.m */; }; D56436492930BC1300E2B633 /* AuthenticationServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D56436472930BC0900E2B633 /* AuthenticationServices.framework */; }; @@ -376,6 +377,8 @@ D52A8D9F288E6547006574E8 /* libuv_a.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libuv_a.a; sourceTree = ""; }; D5538BA3287E9908000BDFB6 /* WalletEvent.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WalletEvent.cpp; sourceTree = ""; }; D5538BA4287E9908000BDFB6 /* WalletEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WalletEvent.h; sourceTree = ""; }; + D5608AF729348B83007F146A /* NSString+Customer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSString+Customer.h"; sourceTree = ""; }; + D5608AF829348B83007F146A /* NSString+Customer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+Customer.m"; sourceTree = ""; }; D56436402930AAF700E2B633 /* UIView+Toast.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIView+Toast.h"; sourceTree = ""; }; D56436412930AAF700E2B633 /* UIView+Toast.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIView+Toast.m"; sourceTree = ""; }; D56436442930ABAB00E2B633 /* UIUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIUtils.h; sourceTree = ""; }; @@ -977,6 +980,8 @@ D5AB1D4228C0539600AA6AFA /* UIViewController+Wallet.mm */, D5D9BAF7293477E700F18A7F /* UIViewController+QR.h */, D5D9BAF8293477E700F18A7F /* UIViewController+QR.mm */, + D5608AF729348B83007F146A /* NSString+Customer.h */, + D5608AF829348B83007F146A /* NSString+Customer.m */, D5F2D105287C12DD003C2B62 /* JcWallet.h */, D5F2D104287C12DD003C2B62 /* JcWallet.mm */, D5538BA4287E9908000BDFB6 /* WalletEvent.h */, @@ -1281,6 +1286,7 @@ 8AC71EC419E7FBA90027502F /* OrientationSupport.mm in Sources */, D5F2CFA5287BEC0D003C2B62 /* Bulk_Assembly-CSharp_0.cpp in Sources */, 8A7939FD1ED2F53200B44EF1 /* UnityViewControllerBase.mm in Sources */, + D5608AF929348B83007F146A /* NSString+Customer.m in Sources */, D5F2CFA1287BEC0D003C2B62 /* Il2CppGenericMethodDefinitions.cpp in Sources */, D5F2CF60287BEC0D003C2B62 /* Bulk_UnityEngine.PhysicsModule_0.cpp in Sources */, D5F2CF93287BEC0D003C2B62 /* Bulk_UnityEngine.AnimationModule_0.cpp in Sources */, diff --git a/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/UserInterfaceState.xcuserstate b/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/UserInterfaceState.xcuserstate index 300a279..e6db2a4 100644 Binary files a/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/UserInterfaceState.xcuserstate and b/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index e29978b..3ed37f8 100644 --- a/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Unity-iPhone.xcworkspace/xcuserdata/zhl.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -72,14 +72,14 @@ BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">