From 48637e05b6ed576ea116b6a18c5f586fcfbcd303 Mon Sep 17 00:00:00 2001 From: Brian Neisler Date: Tue, 19 Jan 2016 13:28:01 -0800 Subject: [PATCH 1/2] Add hide method to react-native-lock-ios --- auth0-lock.js | 4 ++++ objc/A0LockReactModule.m | 6 ++++++ objc/core/A0LockReact.h | 2 ++ objc/core/A0LockReact.m | 20 ++++++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/auth0-lock.js b/auth0-lock.js index 63bb6b6..feef59c 100644 --- a/auth0-lock.js +++ b/auth0-lock.js @@ -14,6 +14,10 @@ class Auth0Lock { } } + hide(callback) { + LockModule.hide(callback); + } + show(options, callback) { LockModule.init(this.lockOptions); if (this.nativeIntegrations) { diff --git a/objc/A0LockReactModule.m b/objc/A0LockReactModule.m index af0bb80..bc19280 100644 --- a/objc/A0LockReactModule.m +++ b/objc/A0LockReactModule.m @@ -85,6 +85,12 @@ @implementation A0LockReactModule [lock registerAuthenticators:authenticators]; } +RCT_EXPORT_METHOD(hide:(RCTResponseSenderBlock)callback) { + dispatch_async(dispatch_get_main_queue(), ^{ + [[A0LockReact sharedInstance] hideWithCallback:callback]; + }); +} + RCT_EXPORT_METHOD(show:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) { dispatch_async(dispatch_get_main_queue(), ^{ [[A0LockReact sharedInstance] showWithOptions:options callback:callback]; diff --git a/objc/core/A0LockReact.h b/objc/core/A0LockReact.h index 9de75b7..53c9420 100644 --- a/objc/core/A0LockReact.h +++ b/objc/core/A0LockReact.h @@ -29,6 +29,8 @@ typedef void(^A0LockCallback)(NSArray *parameters); @property (strong, readonly, nonatomic) A0Lock *lock; +- (void)hideWithCallback:(A0LockCallback)callback; + - (void)showWithOptions:(NSDictionary *)options callback:(A0LockCallback)callback; - (void)configureLockFromBundle; diff --git a/objc/core/A0LockReact.m b/objc/core/A0LockReact.m index af2e2f0..8218cce 100644 --- a/objc/core/A0LockReact.m +++ b/objc/core/A0LockReact.m @@ -25,6 +25,11 @@ #import "A0Token+ReactNative.h" #import "A0UserProfile+ReactNative.h" + +@interface A0LockReact() +@property (nonatomic, assign) BOOL shown; +@end + @implementation A0LockReact + (instancetype)sharedInstance { @@ -44,6 +49,19 @@ - (void)configureLockWithClientId:(NSString *)clientId domain:(NSString *)domain _lock = [A0Lock newLockWithClientId:clientId domain:domain]; } +- (void)hideWithCallback:(A0LockCallback)callback { + if (self.shown) { + if (!self.lock) { + callback(@[@"Please configure Lock before using it"]); + return; + } + UIViewController *controller = [[[[UIApplication sharedApplication] windows] firstObject] rootViewController]; + [controller dismissViewControllerAnimated:YES completion:nil]; + self.shown = NO; + } + callback(@[]); +} + - (void)showWithOptions:(NSDictionary *)options callback:(A0LockCallback)callback { static NSString *TouchID = @"touchid"; static NSString *SMS = @"sms"; @@ -78,6 +96,7 @@ - (void)showWithOptions:(NSDictionary *)options callback:(A0LockCallback)callbac [controller dismissViewControllerAnimated:YES completion:nil]; }; void(^dismissBlock)() = ^{ + self.shown = NO; callback(@[@"Lock was dismissed by the user", [NSNull null], [NSNull null]]); }; @@ -115,6 +134,7 @@ - (void)showWithOptions:(NSDictionary *)options callback:(A0LockCallback)callbac lock.onUserDismissBlock = dismissBlock; [self.lock presentLockController:lock fromController:controller]; } + self.shown = YES; } - (A0AuthParameters *)authenticationParametersFromOptions:(NSDictionary *)options { From a9f823666890415943e401d2b1578a8c266dbc9c Mon Sep 17 00:00:00 2001 From: Brian Neisler Date: Wed, 20 Jan 2016 15:02:24 -0800 Subject: [PATCH 2/2] Move callback execution to completion handler of dismissViewControllerAnimated --- objc/core/A0LockReact.m | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/objc/core/A0LockReact.m b/objc/core/A0LockReact.m index 8218cce..2a242f3 100644 --- a/objc/core/A0LockReact.m +++ b/objc/core/A0LockReact.m @@ -56,10 +56,13 @@ - (void)hideWithCallback:(A0LockCallback)callback { return; } UIViewController *controller = [[[[UIApplication sharedApplication] windows] firstObject] rootViewController]; - [controller dismissViewControllerAnimated:YES completion:nil]; - self.shown = NO; + [controller dismissViewControllerAnimated:YES completion:^{ + self.shown = NO; + callback(@[]); + }]; + } else { + callback(@[]); } - callback(@[]); } - (void)showWithOptions:(NSDictionary *)options callback:(A0LockCallback)callback {