/
RNSwanBrowser.mm
129 lines (99 loc) · 3.84 KB
/
RNSwanBrowser.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#import "RNSwanBrowser.h"
#import <React/RCTConvert.h>
#import <SafariServices/SafariServices.h>
@interface RNSwanBrowser() <SFSafariViewControllerDelegate, UIAdaptivePresentationControllerDelegate>
@property (nonatomic, strong) SFSafariViewController *safariVC;
@end
@implementation RNSwanBrowser {
bool hasListeners;
}
RCT_EXPORT_MODULE();
+ (BOOL)requiresMainQueueSetup {
return NO;
}
- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
- (void)startObserving {
hasListeners = YES;
}
- (void)stopObserving {
hasListeners = NO;
}
- (NSArray<NSString *> *)supportedEvents {
return @[@"swanBrowserDidClose"];
}
- (void)handleOnClose {
_safariVC = nil;
if (hasListeners) {
[self sendEventWithName:@"swanBrowserDidClose" body:nil];
}
}
- (void)presentationControllerDidDismiss:(UIPresentationController *)controller {
[self handleOnClose];
}
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
[self handleOnClose];
}
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeRNSwanBrowserSpecJSI>(params);
}
- (void)open:(NSString *)url
options:(JS::NativeRNSwanBrowser::Options &)options
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject {
NSString *dismissButtonStyle = options.dismissButtonStyle();
NSNumber *barTintColor = options.barTintColor().has_value() ? [NSNumber numberWithDouble:options.barTintColor().value()] : nil;
NSNumber *controlTintColor = options.controlTintColor().has_value() ? [NSNumber numberWithDouble:options.controlTintColor().value()] : nil;
#else
RCT_EXPORT_METHOD(open:(NSString *)url
options:(NSDictionary * _Nonnull)options
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
NSString *dismissButtonStyle = [options valueForKey:@"dismissButtonStyle"];
NSNumber *barTintColor = [options valueForKey:@"barTintColor"];
NSNumber *controlTintColor = [options valueForKey:@"controlTintColor"];
#endif
if (_safariVC != nil) {
return reject(@"swan_browser_visible", @"An instance of the swan browser is already visible", nil);
}
@try {
SFSafariViewControllerConfiguration *config = [SFSafariViewControllerConfiguration new];
[config setBarCollapsingEnabled:false];
[config setEntersReaderIfAvailable:false];
_safariVC = [[SFSafariViewController alloc] initWithURL:[[NSURL alloc] initWithString:url] configuration:config];
if (dismissButtonStyle == nil) {
[_safariVC setDismissButtonStyle:SFSafariViewControllerDismissButtonStyleClose];
} else if ([dismissButtonStyle isEqualToString:@"cancel"]) {
[_safariVC setDismissButtonStyle:SFSafariViewControllerDismissButtonStyleCancel];
} else if ([dismissButtonStyle isEqualToString:@"done"]) {
[_safariVC setDismissButtonStyle:SFSafariViewControllerDismissButtonStyleDone];
}
if (barTintColor != nil) {
[_safariVC setPreferredBarTintColor:[RCTConvert UIColor:barTintColor]];
}
if (controlTintColor != nil) {
[_safariVC setPreferredControlTintColor:[RCTConvert UIColor:controlTintColor]];
}
[_safariVC setModalPresentationStyle:UIModalPresentationPageSheet];
[RCTPresentedViewController() presentViewController:_safariVC animated:true completion:nil];
_safariVC.delegate = self;
_safariVC.presentationController.delegate = self;
resolve(nil);
} @catch (NSException *exception) {
reject(exception.name, exception.reason, nil);
}
}
#ifdef RCT_NEW_ARCH_ENABLED
- (void)close {
#else
RCT_EXPORT_METHOD(close) {
#endif
if (_safariVC != nil) {
[RCTPresentedViewController() dismissViewControllerAnimated:true completion:^{
[self handleOnClose];
}];
}
}
@end