forked from naoufal/react-native-safari-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafariViewManager.m
120 lines (97 loc) · 3.62 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
#import "SafariViewManager.h"
#import <React/RCTUtils.h>
#import <React/RCTLog.h>
#import <React/RCTConvert.h>
@implementation SafariViewManager
{
bool hasListeners;
}
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;
}
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"];
BOOL fromBottom = [args[@"fromBottom"] boolValue];
// Initialize the Safari View
_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:)]) {
[_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;
}
UIViewController *ctrl = [[[UIApplication sharedApplication] keyWindow] rootViewController];
// Cycle through view controllers to get the view closest to the foreground
while (ctrl.presentedViewController && !ctrl.isBeingDismissed) {
ctrl = ctrl.presentedViewController;
}
// Display the Safari View
[ctrl presentViewController: _safariView animated:YES completion:nil];
if (hasListeners) {
[self sendEventWithName:@"SafariViewOnShow" body:nil];
}
resolve(@YES);
}
RCT_EXPORT_METHOD(isAvailable:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if (@available(iOS 9.0, *)) {
resolve(@YES);
} else {
reject(@"E_SAFARI_VIEW_UNAVAILABLE", @"SafariView is unavailable", nil);
}
}
RCT_EXPORT_METHOD(dismiss)
{
[_safariView dismissViewControllerAnimated:true completion:nil];
}
-(void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller
{
_safariView = nil;
NSLog(@"[SafariView] SafariView dismissed.");
if (hasListeners) {
[self sendEventWithName:@"SafariViewOnDismiss" body:nil];
}
}
@end