33#import < React/RCTConvert.h>
44#import < SafariServices/SafariServices.h>
55
6+ @interface RNSwanBrowser () <SFSafariViewControllerDelegate, UIAdaptivePresentationControllerDelegate>
7+
8+ @property (nonatomic , strong ) SFSafariViewController *safariVC;
9+
10+ @end
11+
612@implementation RNSwanBrowser
713
814RCT_EXPORT_MODULE ();
@@ -15,6 +21,18 @@ - (dispatch_queue_t)methodQueue {
1521 return dispatch_get_main_queue ();
1622}
1723
24+ - (void )handleOnClose {
25+ _safariVC = nil ;
26+ }
27+
28+ - (void )presentationControllerDidDismiss : (UIPresentationController *)controller {
29+ [self handleOnClose ];
30+ }
31+
32+ - (void )safariViewControllerDidFinish : (SFSafariViewController *)controller {
33+ [self handleOnClose ];
34+ }
35+
1836#ifdef RCT_NEW_ARCH_ENABLED
1937- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule : (const facebook::react::ObjCTurboModule::InitParams &)params {
2038 return std::make_shared<facebook::react::NativeRNSwanBrowserSpecJSI>(params);
@@ -42,41 +60,61 @@ - (void)open:(NSString *)url
4260 NSNumber *controlTintColor = [options valueForKey: @" controlTintColor" ];
4361
4462#endif
63+ if (_safariVC != nil ) {
64+ return reject (@" swan_browser_visible" , @" An instance of the swan browser is already visible" , nil );
65+ }
66+
4567 @try {
4668 SFSafariViewControllerConfiguration *config = [SFSafariViewControllerConfiguration new ];
4769 [config setBarCollapsingEnabled: false ];
4870 [config setEntersReaderIfAvailable: false ];
4971
50- SFSafariViewController *safari = [[SFSafariViewController alloc ] initWithURL: [[NSURL alloc ] initWithString: url] configuration: config];
72+ _safariVC = [[SFSafariViewController alloc ] initWithURL: [[NSURL alloc ] initWithString: url] configuration: config];
5173
5274 if (dismissButtonStyle == nil || [dismissButtonStyle isEqualToString: @" close" ]) {
53- [safari setDismissButtonStyle: SFSafariViewControllerDismissButtonStyleClose];
75+ [_safariVC setDismissButtonStyle: SFSafariViewControllerDismissButtonStyleClose];
5476 } else if ([dismissButtonStyle isEqualToString: @" cancel" ]) {
55- [safari setDismissButtonStyle: SFSafariViewControllerDismissButtonStyleCancel];
77+ [_safariVC setDismissButtonStyle: SFSafariViewControllerDismissButtonStyleCancel];
5678 } else if ([dismissButtonStyle isEqualToString: @" done" ]) {
57- [safari setDismissButtonStyle: SFSafariViewControllerDismissButtonStyleDone];
79+ [_safariVC setDismissButtonStyle: SFSafariViewControllerDismissButtonStyleDone];
5880 }
5981
6082 if (barTintColor != nil ) {
61- [safari setPreferredBarTintColor: [RCTConvert UIColor: barTintColor]];
83+ [_safariVC setPreferredBarTintColor: [RCTConvert UIColor: barTintColor]];
6284 }
6385 if (controlTintColor != nil ) {
64- [safari setPreferredControlTintColor: [RCTConvert UIColor: controlTintColor]];
86+ [_safariVC setPreferredControlTintColor: [RCTConvert UIColor: controlTintColor]];
6587 }
6688
6789 if (animationType != nil && [animationType isEqualToString: @" fade" ]) {
68- [safari setModalPresentationStyle: UIModalPresentationOverFullScreen];
69- [safari setModalTransitionStyle: UIModalTransitionStyleCrossDissolve];
90+ [_safariVC setModalPresentationStyle: UIModalPresentationOverFullScreen];
91+ [_safariVC setModalTransitionStyle: UIModalTransitionStyleCrossDissolve];
7092 } else {
71- [safari setModalPresentationStyle: UIModalPresentationPageSheet];
93+ [_safariVC setModalPresentationStyle: UIModalPresentationPageSheet];
7294 }
7395
74- [RCTPresentedViewController () presentViewController: safari animated: true completion: nil ];
96+ [RCTPresentedViewController () presentViewController: _safariVC animated: true completion: nil ];
97+
98+ _safariVC.delegate = self;
99+ _safariVC.presentationController .delegate = self;
75100
76101 resolve (nil );
77102 } @catch (NSException *exception) {
103+ _safariVC = nil ;
78104 reject (exception.name , exception.reason , nil );
79105 }
80106}
81107
108+ #ifdef RCT_NEW_ARCH_ENABLED
109+ - (void )close {
110+ #else
111+ RCT_EXPORT_METHOD (close) {
112+ #endif
113+ if (_safariVC != nil ) {
114+ [RCTPresentedViewController () dismissViewControllerAnimated: true completion: ^{
115+ [self handleOnClose ];
116+ }];
117+ }
118+ }
119+
82120@end
0 commit comments