/
EXWebBrowser.m
281 lines (244 loc) · 9.83 KB
/
EXWebBrowser.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2015-present 650 Industries. All rights reserved.
#import <SafariServices/SafariServices.h>
#import <EXWebBrowser/EXWebBrowser.h>
#import <UMCore/UMUtilities.h>
static NSString* const WebBrowserErrorCode = @"WebBrowser";
static NSString* const WebBrowserControlsColorKey = @"controlsColor";
static NSString* const WebBrowserToolbarColorKey = @"toolbarColor";
@interface EXWebBrowser () <SFSafariViewControllerDelegate>
@property (nonatomic, copy) UMPromiseResolveBlock redirectResolve;
@property (nonatomic, copy) UMPromiseRejectBlock redirectReject;
@property (nonatomic, weak) UMModuleRegistry *moduleRegistry;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
@property (nonatomic, strong) SFAuthenticationSession *authSession;
#pragma clang diagnostic pop
@end
@implementation EXWebBrowser
{
UIStatusBarStyle _initialStatusBarStyle;
}
UM_EXPORT_MODULE(ExpoWebBrowser)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
UM_EXPORT_METHOD_AS(openAuthSessionAsync,
openAuthSessionAsync:(NSString *)authURL
redirectURL:(NSString *)redirectURL
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
[self initializeWebBrowserWithResolver:resolve andRejecter:reject];
if (@available(iOS 11, *)) {
NSURL *url = [[NSURL alloc] initWithString: authURL];
__weak typeof(self) weakSelf = self;
void (^completionHandler)(NSURL * _Nullable, NSError *_Nullable) = ^(NSURL* _Nullable callbackURL, NSError* _Nullable error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
if (!error) {
NSString *url = callbackURL.absoluteString;
strongSelf->_redirectResolve(@{
@"type" : @"success",
@"url" : url,
});
} else {
strongSelf->_redirectResolve(@{
@"type" : @"cancel",
});
}
[strongSelf flowDidFinish];
}
};
_authSession = [[SFAuthenticationSession alloc]
initWithURL:url
callbackURLScheme:redirectURL
completionHandler:completionHandler];
[_authSession start];
} else {
resolve(@{
@"type" : @"cancel",
@"message" : @"openAuthSessionAsync requires iOS 11 or greater"
});
[self flowDidFinish];
}
}
UM_EXPORT_METHOD_AS(openBrowserAsync,
openBrowserAsync:(NSString *)authURL
withArguments:(NSDictionary *)arguments
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
if (![self initializeWebBrowserWithResolver:resolve andRejecter:reject]) {
return;
}
NSURL *url = [[NSURL alloc] initWithString:authURL];
BOOL readerMode = [arguments[@"readerMode"] boolValue];
BOOL enableBarCollapsing = [arguments[@"enableBarCollapsing"] boolValue];
SFSafariViewController *safariVC = nil;
if (@available(iOS 11, *)) {
SFSafariViewControllerConfiguration *config = [[SFSafariViewControllerConfiguration alloc] init];
config.barCollapsingEnabled = enableBarCollapsing;
config.entersReaderIfAvailable = readerMode;
safariVC = [[SFSafariViewController alloc] initWithURL:url configuration:config];
} else {
safariVC = [[SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:readerMode];
}
if (@available(iOS 11.0, *)) {
NSString *dismissButtonStyle = [arguments valueForKey:@"dismissButtonStyle"];
if ([@"done" isEqualToString:dismissButtonStyle]) {
safariVC.dismissButtonStyle = SFSafariViewControllerDismissButtonStyleDone;
}
else if ([@"close" isEqualToString:dismissButtonStyle]) {
safariVC.dismissButtonStyle = SFSafariViewControllerDismissButtonStyleClose;
}
else if ([@"cancel" isEqualToString:dismissButtonStyle]) {
safariVC.dismissButtonStyle = SFSafariViewControllerDismissButtonStyleCancel;
}
}
if([[arguments allKeys] containsObject:WebBrowserToolbarColorKey]) {
safariVC.preferredBarTintColor = [EXWebBrowser convertHexColorString:arguments[WebBrowserToolbarColorKey]];
}
if([[arguments allKeys] containsObject:WebBrowserControlsColorKey]) {
safariVC.preferredControlTintColor = [EXWebBrowser convertHexColorString:arguments[WebBrowserControlsColorKey]];
}
safariVC.delegate = self;
// By setting the modal presentation style to OverFullScreen, we disable the "Swipe to dismiss"
// gesture that is causing a bug where sometimes `safariViewControllerDidFinish` is not called.
// There are bugs filed already about it on OpenRadar.
[safariVC setModalPresentationStyle: UIModalPresentationOverFullScreen];
// This is a hack to present the SafariViewController modally
UINavigationController *safariHackVC = [[UINavigationController alloc] initWithRootViewController:safariVC];
[safariHackVC setNavigationBarHidden:true animated:false];
[safariHackVC setModalPresentationStyle: UIModalPresentationOverFullScreen];
UIViewController *currentViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (currentViewController.presentedViewController) {
currentViewController = currentViewController.presentedViewController;
}
[currentViewController presentViewController:safariHackVC animated:true completion:nil];
}
UM_EXPORT_METHOD_AS(dismissBrowser,
dismissBrowserWithResolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
__weak typeof(self) weakSelf = self;
UIViewController *currentViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (currentViewController.presentedViewController) {
currentViewController = currentViewController.presentedViewController;
}
[currentViewController dismissViewControllerAnimated:YES completion:^{
resolve(nil);
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
if (strongSelf.redirectResolve) {
strongSelf.redirectResolve(@{
@"type": @"dismiss",
});
}
[strongSelf flowDidFinish];
}
}];
}
UM_EXPORT_METHOD_AS(dismissAuthSession,
dismissAuthSessionWithResolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
if (@available(iOS 11, *)) {
[_authSession cancel];
resolve(nil);
if (_redirectResolve) {
_redirectResolve(@{
@"type": @"dismiss"
});
[self flowDidFinish];
}
} else {
[self dismissAuthSessionWithResolver:resolve rejecter:reject];
}
}
UM_EXPORT_METHOD_AS(warmUpAsync,
warmUpAsyncWithPackage:(NSString*)browserPackage
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
// stub for jest-expo-mock-generator
}
UM_EXPORT_METHOD_AS(coolDownAsync,
coolDownAsyncWithPackage:(NSString*)browserPackage
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
// stub for jest-expo-mock-generator
}
UM_EXPORT_METHOD_AS(getCustomTabsSupportingBrowsers,
getCustomTabsSupportingBrowsersWithPackage:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
// stub for jest-expo-mock-generator
}
UM_EXPORT_METHOD_AS(mayInitWithUrlAsync,
warmUpAsyncWithUrl:(NSString*)url
browserPackage:(NSString*)package
resolver:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
// stub for jest-expo-mock-generator
}
/**
* Helper that is used in openBrowserAsync and openAuthSessionAsync
*/
- (BOOL)initializeWebBrowserWithResolver:(UMPromiseResolveBlock)resolve andRejecter:(UMPromiseRejectBlock)reject {
if (_redirectResolve) {
reject(WebBrowserErrorCode, @"Another WebBrowser is already being presented.", nil);
return NO;
}
_redirectReject = reject;
_redirectResolve = resolve;
_initialStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault
animated:YES];
#pragma clang diagnostic pop
return YES;
}
/**
* Called when the user dismisses the SFVC without logging in.
*/
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller
{
_redirectResolve(@{
@"type": @"cancel",
});
[self flowDidFinish];
}
-(void)flowDidFinish
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] setStatusBarStyle:_initialStatusBarStyle animated:YES];
#pragma clang diagnostic pop
_redirectResolve = nil;
_redirectReject = nil;
}
- (void)setModuleRegistry:(UMModuleRegistry *)moduleRegistry
{
_moduleRegistry = moduleRegistry;
}
+ (UIColor *)convertHexColorString:(NSString *)stringToConvert {
NSString *strippedString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""];
NSScanner *scanner = [NSScanner scannerWithString:strippedString];
unsigned hexNum;
if (![scanner scanHexInt:&hexNum]) return nil;
return [EXWebBrowser colorWithRGBHex:hexNum];
}
+ (UIColor *)colorWithRGBHex:(UInt32)hex {
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;
return [UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:1.0f];
}
@end