From ed6fe52bc29c2d4f21f11118b3350e55d121a9c4 Mon Sep 17 00:00:00 2001 From: Chaithanya Prathyush Date: Thu, 30 Nov 2017 19:05:13 +0530 Subject: [PATCH 1/6] Use the ivar provided by obj-c instead of referring to self. - This leverages ARC features provided by obj-c, and also reduces side effects. refer - https://stackoverflow.com/questions/21844620/difference-between-using-self-variable-and-variable-when-init-these-variables --- SafariViewManager.m | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/SafariViewManager.m b/SafariViewManager.m index e26be0c..d453982 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -46,37 +46,37 @@ - (void)stopObserving BOOL fromBottom = [args[@"fromBottom"] boolValue]; // Initialize the Safari View - self.safariView = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode]; - self.safariView.delegate = self; + _safariView = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode]; + _safariView.delegate = self; // Set tintColor if available if (tintColorString) { UIColor *tintColor = [RCTConvert UIColor:tintColorString]; - if ([self.safariView respondsToSelector:@selector(setPreferredControlTintColor:)]) { - [self.safariView setPreferredControlTintColor:tintColor]; + if ([_safariView respondsToSelector:@selector(setPreferredControlTintColor:)]) { + [_safariView setPreferredControlTintColor:tintColor]; } else { - [self.safariView.view setTintColor:tintColor]; + [_safariView.view setTintColor:tintColor]; } } // Set barTintColor if available if (barTintColorString) { UIColor *barTintColor = [RCTConvert UIColor:barTintColorString]; - if ([self.safariView respondsToSelector:@selector(setPreferredBarTintColor:)]) { - [self.safariView setPreferredBarTintColor:barTintColor]; + if ([_safariView respondsToSelector:@selector(setPreferredBarTintColor:)]) { + [_safariView setPreferredBarTintColor:barTintColor]; } } // Set modal transition style if (fromBottom) { - self.safariView.modalPresentationStyle = UIModalPresentationOverFullScreen; + _safariView.modalPresentationStyle = UIModalPresentationOverFullScreen; } // get the view controller closest to the foreground UIViewController *ctrl = RCTPresentedViewController(); // Display the Safari View - [ctrl presentViewController:self.safariView animated:YES completion:nil]; + [ctrl presentViewController:_safariView animated:YES completion:nil]; if (hasListeners) { [self sendEventWithName:@"SafariViewOnShow" body:nil]; @@ -97,7 +97,7 @@ - (void)stopObserving RCT_EXPORT_METHOD(dismiss) { - [self safariViewControllerDidFinish:self.safariView]; + [self safariViewControllerDidFinish:_safariView]; } -(void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller @@ -109,5 +109,4 @@ -(void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controlle [self sendEventWithName:@"SafariViewOnDismiss" body:nil]; } } - @end From 8d4b5f7e0bcf14574fa206e4f4d9f920e63b4133 Mon Sep 17 00:00:00 2001 From: Chaithanya Prathyush Date: Thu, 30 Nov 2017 19:08:27 +0530 Subject: [PATCH 2/6] - Check for escaped characters in the URL string from args, before constructing the URL --- SafariViewManager.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SafariViewManager.m b/SafariViewManager.m index d453982..49e3327 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -39,7 +39,12 @@ - (void)stopObserving return; } - NSURL *url = [NSURL URLWithString:args[@"url"]]; + NSMutableString *tempStr = [NSMutableString stringWithString:args[@"url"]]; + [tempStr replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])]; + [tempStr replaceOccurrencesOfString:@"%09%09" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])]; + NSString *urlString = [[NSString stringWithFormat:@"%@",tempStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSURL *url = [NSURL URLWithString: urlString]; + BOOL readerMode = [args[@"readerMode"] boolValue]; UIColor *tintColorString = args[@"tintColor"]; UIColor *barTintColorString = args[@"barTintColor"]; From 33dc17cce648ef1b421a50cfa40859ffbf59a653 Mon Sep 17 00:00:00 2001 From: Chaithanya Prathyush Date: Thu, 30 Nov 2017 19:16:41 +0530 Subject: [PATCH 3/6] - Refactor availability checking for SFSafariViewController Safari View Controller is available after iOS 9, iOS version check is enough to determine the availability --- SafariViewManager.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SafariViewManager.m b/SafariViewManager.m index 49e3327..79bef8f 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -92,7 +92,7 @@ - (void)stopObserving RCT_EXPORT_METHOD(isAvailable:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - if ([SFSafariViewController class]) { + if (@available(iOS 9.0, *)) { // SafariView is available resolve(@YES); } else { From 7b3de7217e8fbb49c367a24661e35042d17fbe99 Mon Sep 17 00:00:00 2001 From: Chaithanya Prathyush Date: Thu, 30 Nov 2017 19:19:31 +0530 Subject: [PATCH 4/6] =?UTF-8?q?-=20Dismiss=20the=20SafariView=20directly?= =?UTF-8?q?=20by=20calling=20=E2=80=98dismissViewControllerAnimated:=20com?= =?UTF-8?q?pletion:=E2=80=99=20instead=20of=20calling=20the=20delegate=20w?= =?UTF-8?q?hich=20inturn=20calls=20the=20dismiss=20not=20the=20view=20cont?= =?UTF-8?q?roller=20-=20Deallocate=20Safari=20View,=20as=20it=20is=20no=20?= =?UTF-8?q?longer=20required=20after=20dismissal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SafariViewManager.m | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/SafariViewManager.m b/SafariViewManager.m index 79bef8f..0a59d17 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -102,14 +102,13 @@ - (void)stopObserving RCT_EXPORT_METHOD(dismiss) { - [self safariViewControllerDidFinish:_safariView]; + [_safariView dismissViewControllerAnimated:true completion:nil]; } -(void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller { - [controller dismissViewControllerAnimated:true completion:nil]; + _safariView = nil; NSLog(@"[SafariView] SafariView dismissed."); - if (hasListeners) { [self sendEventWithName:@"SafariViewOnDismiss" body:nil]; } From 64f071d2c709c111ef41d657b184ea9e7651c15e Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 3 Jan 2018 14:59:27 +0100 Subject: [PATCH 5/6] use RCTConvert method to convert URL --- SafariViewManager.m | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/SafariViewManager.m b/SafariViewManager.m index 0a59d17..eb89d54 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -39,12 +39,7 @@ - (void)stopObserving return; } - NSMutableString *tempStr = [NSMutableString stringWithString:args[@"url"]]; - [tempStr replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])]; - [tempStr replaceOccurrencesOfString:@"%09%09" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])]; - NSString *urlString = [[NSString stringWithFormat:@"%@",tempStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; - NSURL *url = [NSURL URLWithString: urlString]; - + NSURL *url = [RCTConvert NSURL:args[@"url"]]; BOOL readerMode = [args[@"readerMode"] boolValue]; UIColor *tintColorString = args[@"tintColor"]; UIColor *barTintColorString = args[@"barTintColor"]; From 8a4d8d00be5e447097b9229b31ab2f2a180de57a Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 3 Jan 2018 15:49:03 +0100 Subject: [PATCH 6/6] remove property for safariview since the property is accessed outside of this module there's no point in having it as property --- SafariViewManager.h | 2 -- SafariViewManager.m | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/SafariViewManager.h b/SafariViewManager.h index 69ca8e8..98aaf22 100644 --- a/SafariViewManager.h +++ b/SafariViewManager.h @@ -5,6 +5,4 @@ @interface SafariViewManager : RCTEventEmitter -@property (nonatomic) SFSafariViewController *safariView; - @end diff --git a/SafariViewManager.m b/SafariViewManager.m index eb89d54..b606e2d 100644 --- a/SafariViewManager.m +++ b/SafariViewManager.m @@ -7,6 +7,7 @@ @implementation SafariViewManager { bool hasListeners; + SFSafariViewController *_safariView; } RCT_EXPORT_MODULE()