forked from naoufal/react-native-safari-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafariViewManager.m
145 lines (122 loc) · 4.28 KB
/
SafariViewManager.m
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#import "SafariViewManager.h"
#import <React/RCTUtils.h>
#import <React/RCTLog.h>
#import <React/RCTConvert.h>
@implementation SafariViewManager
{
bool hasListeners;
SFSafariViewController *_safariView;
}
RCT_EXPORT_MODULE()
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (void)startObserving
{
hasListeners = YES;
}
- (void)stopObserving
{
hasListeners = NO;
}
- (NSArray<NSString *> *)supportedEvents
{
return @[@"SafariViewOnShow", @"SafariViewOnDismiss"];
}
RCT_EXPORT_METHOD(show:(NSDictionary *)args resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
// Error if no url is passed
if (!args[@"url"]) {
reject(@"E_SAFARI_VIEW_NO_URL", @"You must specify a url.", nil);
return;
}
NSURL *url = [RCTConvert NSURL:args[@"url"]];
BOOL readerMode = [args[@"readerMode"] boolValue];
UIColor *tintColorString = args[@"tintColor"];
UIColor *barTintColorString = args[@"barTintColor"];
BOOL fromBottom = [args[@"fromBottom"] boolValue];
NSString *dismissButtonStyleString = args[@"dismissButtonStyle"];
BOOL preferNative = [args[@"preferNative"] boolValue];
// Initialize the Safari View
_safariView = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode];
_safariView.delegate = self;
// set dismiss button style
if ([_safariView respondsToSelector:@selector(setDismissButtonStyle:)] ) {
if (@available(iOS 11.0, *)) {
if ([dismissButtonStyleString isEqual:@"Close"]) {
[_safariView setDismissButtonStyle:SFSafariViewControllerDismissButtonStyleClose];
} else if ([dismissButtonStyleString isEqual:@"Cancel"]) {
[_safariView setDismissButtonStyle:SFSafariViewControllerDismissButtonStyleCancel];
} else {
[_safariView setDismissButtonStyle:SFSafariViewControllerDismissButtonStyleDone];
}
}
}
// Set tintColor if available
if (tintColorString) {
UIColor *tintColor = [RCTConvert UIColor:tintColorString];
if ([_safariView respondsToSelector:@selector(setPreferredControlTintColor:)]) {
[_safariView setPreferredControlTintColor:tintColor];
} else {
[_safariView.view setTintColor:tintColor];
}
}
// Set barTintColor if available
if (barTintColorString) {
UIColor *barTintColor = [RCTConvert UIColor:barTintColorString];
if ([_safariView respondsToSelector:@selector(setPreferredBarTintColor:)]) {
[_safariView setPreferredBarTintColor:barTintColor];
}
}
// Set modal transition style
if (fromBottom) {
_safariView.modalPresentationStyle = UIModalPresentationOverFullScreen;
}
if (!preferNative) {
[self openInSafariView];
resolve(@YES);
return;
}
[[UIApplication sharedApplication] openURL:url
options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @YES}
completionHandler:^(BOOL success) {
if (!success) {
[self openInSafariView];
}
resolve(@YES);
}
];
}
RCT_EXPORT_METHOD(isAvailable:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if (@available(iOS 9.0, *)) {
// SafariView is available
resolve(@YES);
} else {
reject(@"E_SAFARI_VIEW_UNAVAILABLE", @"SafariView is unavailable", nil);
}
}
RCT_EXPORT_METHOD(dismiss)
{
[_safariView dismissViewControllerAnimated:true completion:nil];
}
-(void)openInSafariView
{
// get the view controller closest to the foreground
UIViewController *ctrl = RCTPresentedViewController();
// Display the Safari View
[ctrl presentViewController:_safariView animated:YES completion:nil];
if (hasListeners) {
[self sendEventWithName:@"SafariViewOnShow" body:nil];
}
}
-(void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller
{
_safariView = nil;
NSLog(@"[SafariView] SafariView dismissed.");
if (hasListeners) {
[self sendEventWithName:@"SafariViewOnDismiss" body:nil];
}
}
@end