From b439e1f3cb12a35a409453071a3f08da2902d398 Mon Sep 17 00:00:00 2001 From: Stephen Bird Date: Fri, 25 Aug 2017 13:34:34 -0700 Subject: [PATCH] iOS: Fix opening of URL for webauth Fixes occasional issue with SFSafariViewController where the view fails to open with the following warning: Warning: Attempt to present on which is already presenting --- ios/A0Auth0.m | 50 ++++++++------------------------------------------ 1 file changed, 8 insertions(+), 42 deletions(-) diff --git a/ios/A0Auth0.m b/ios/A0Auth0.m index db9f883d..1f03a41a 100644 --- a/ios/A0Auth0.m +++ b/ios/A0Auth0.m @@ -1,7 +1,6 @@ #import "A0Auth0.h" -#import #import #if __has_include("RCTUtils.h") @@ -10,14 +9,12 @@ #import #endif -@interface A0Auth0 () -@property (weak, nonatomic) SFSafariViewController *last; +@interface A0Auth0 () @property (copy, nonatomic) RCTResponseSenderBlock sessionCallback; @property (assign, nonatomic) BOOL closeOnLoad; @end @implementation A0Auth0 - - (dispatch_queue_t)methodQueue { return dispatch_get_main_queue(); @@ -26,7 +23,7 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_MODULE(); RCT_EXPORT_METHOD(hide) { - [self terminateWithError:nil dismissing:YES animated:YES]; + [self terminateWithError:nil]; } RCT_EXPORT_METHOD(showUrl:(NSString *)urlString closeOnLoad:(BOOL)closeOnLoad callback:(RCTResponseSenderBlock)callback) { @@ -46,28 +43,18 @@ - (NSDictionary *)constantsToExport { #pragma mark - Internal methods - (void)presentSafariWithURL:(NSURL *)url { - UIWindow *window = [[UIApplication sharedApplication] keyWindow]; - SFSafariViewController *controller = [[SFSafariViewController alloc] initWithURL:url]; - controller.delegate = self; - [self terminateWithError:RCTMakeError(@"Only one Safari can be visible", nil, nil) dismissing:YES animated:NO]; - [window.rootViewController presentViewController:controller animated:YES completion:nil]; - self.last = controller; + BOOL opend = [RCTSharedApplication() openURL:url]; + if (!opend) { + [self terminateWithError:RCTMakeError(@"Failed to open URL", nil, nil)]; + } } -- (void)terminateWithError:(id)error dismissing:(BOOL)dismissing animated:(BOOL)animated { +- (void)terminateWithError:(id)error { RCTResponseSenderBlock callback = self.sessionCallback ? self.sessionCallback : ^void(NSArray *_unused) {}; - if (dismissing) { - [self.last.presentingViewController dismissViewControllerAnimated:animated - completion:^{ - if (error) { - callback(@[error]); - } - }]; - } else if (error) { + if (error) { callback(@[error]); } self.sessionCallback = nil; - self.last = nil; self.closeOnLoad = NO; } @@ -115,25 +102,4 @@ - (NSDictionary *)generateOAuthParameters { }; } -#pragma mark - SFSafariViewControllerDelegate - -- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller { - NSDictionary *error = @{ - @"error": @"a0.session.user_cancelled", - @"error_description": @"User cancelled the Auth" - }; - [self terminateWithError:error dismissing:NO animated:NO]; -} - -- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully { - if (self.closeOnLoad && didLoadSuccessfully) { - [self terminateWithError:[NSNull null] dismissing:YES animated:YES]; - } else if (!didLoadSuccessfully) { - NSDictionary *error = @{ - @"error": @"a0.session.failed_load", - @"error_description": @"Failed to load url" - }; - [self terminateWithError:error dismissing:YES animated:YES]; - } -} @end