|
@@ -0,0 +1,343 @@
|
|
1
|
+#import "RNCWebView.h"
|
|
2
|
+
|
|
3
|
+// #import <UIKit/UIKit.h>
|
|
4
|
+#import <React/RCTAutoInsetsProtocol.h>
|
|
5
|
+#import <React/RCTConvert.h>
|
|
6
|
+#import <React/RCTEventDispatcher.h>
|
|
7
|
+#import <React/RCTLog.h>
|
|
8
|
+#import <React/RCTUtils.h>
|
|
9
|
+#import <React/RCTView.h>
|
|
10
|
+#import <React/UIView+React.h>
|
|
11
|
+
|
|
12
|
+NSString *const RNCJSNavigationScheme = @"react-js-navigation";
|
|
13
|
+
|
|
14
|
+static NSString *const kPostMessageHost = @"postMessage";
|
|
15
|
+
|
|
16
|
+@interface RNCWebView () <UIWebViewDelegate, RCTAutoInsetsProtocol>
|
|
17
|
+
|
|
18
|
+@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
|
|
19
|
+@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
|
|
20
|
+@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
|
|
21
|
+@property (nonatomic, copy) RCTDirectEventBlock onShouldStartLoadWithRequest;
|
|
22
|
+@property (nonatomic, copy) RCTDirectEventBlock onMessage;
|
|
23
|
+
|
|
24
|
+@end
|
|
25
|
+
|
|
26
|
+@implementation RNCWebView
|
|
27
|
+{
|
|
28
|
+ UIWebView *_webView;
|
|
29
|
+ NSString *_injectedJavaScript;
|
|
30
|
+}
|
|
31
|
+
|
|
32
|
+- (void)dealloc
|
|
33
|
+{
|
|
34
|
+ _webView.delegate = nil;
|
|
35
|
+}
|
|
36
|
+
|
|
37
|
+- (instancetype)initWithFrame:(CGRect)frame
|
|
38
|
+{
|
|
39
|
+ if ((self = [super initWithFrame:frame])) {
|
|
40
|
+ super.backgroundColor = [UIColor clearColor];
|
|
41
|
+ _automaticallyAdjustContentInsets = YES;
|
|
42
|
+ _contentInset = UIEdgeInsetsZero;
|
|
43
|
+ _webView = [[UIWebView alloc] initWithFrame:self.bounds];
|
|
44
|
+ _webView.delegate = self;
|
|
45
|
+#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
|
46
|
+ if ([_webView.scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
|
|
47
|
+ _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
48
|
+ }
|
|
49
|
+#endif
|
|
50
|
+ [self addSubview:_webView];
|
|
51
|
+ }
|
|
52
|
+ return self;
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
|
56
|
+
|
|
57
|
+- (void)goForward
|
|
58
|
+{
|
|
59
|
+ [_webView goForward];
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+- (void)goBack
|
|
63
|
+{
|
|
64
|
+ [_webView goBack];
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+- (void)reload
|
|
68
|
+{
|
|
69
|
+ NSURLRequest *request = [RCTConvert NSURLRequest:self.source];
|
|
70
|
+ if (request.URL && !_webView.request.URL.absoluteString.length) {
|
|
71
|
+ [_webView loadRequest:request];
|
|
72
|
+ }
|
|
73
|
+ else {
|
|
74
|
+ [_webView reload];
|
|
75
|
+ }
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+- (void)stopLoading
|
|
79
|
+{
|
|
80
|
+ [_webView stopLoading];
|
|
81
|
+}
|
|
82
|
+
|
|
83
|
+- (void)postMessage:(NSString *)message
|
|
84
|
+{
|
|
85
|
+ NSDictionary *eventInitDict = @{
|
|
86
|
+ @"data": message,
|
|
87
|
+ };
|
|
88
|
+ NSString *source = [NSString
|
|
89
|
+ stringWithFormat:@"document.dispatchEvent(new MessageEvent('message', %@));",
|
|
90
|
+ RCTJSONStringify(eventInitDict, NULL)
|
|
91
|
+ ];
|
|
92
|
+ [_webView stringByEvaluatingJavaScriptFromString:source];
|
|
93
|
+}
|
|
94
|
+
|
|
95
|
+- (void)injectJavaScript:(NSString *)script
|
|
96
|
+{
|
|
97
|
+ [_webView stringByEvaluatingJavaScriptFromString:script];
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+- (void)setSource:(NSDictionary *)source
|
|
101
|
+{
|
|
102
|
+ if (![_source isEqualToDictionary:source]) {
|
|
103
|
+ _source = [source copy];
|
|
104
|
+
|
|
105
|
+ // Check for a static html source first
|
|
106
|
+ NSString *html = [RCTConvert NSString:source[@"html"]];
|
|
107
|
+ if (html) {
|
|
108
|
+ NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
|
|
109
|
+ if (!baseURL) {
|
|
110
|
+ baseURL = [NSURL URLWithString:@"about:blank"];
|
|
111
|
+ }
|
|
112
|
+ [_webView loadHTMLString:html baseURL:baseURL];
|
|
113
|
+ return;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ NSURLRequest *request = [RCTConvert NSURLRequest:source];
|
|
117
|
+ // Because of the way React works, as pages redirect, we actually end up
|
|
118
|
+ // passing the redirect urls back here, so we ignore them if trying to load
|
|
119
|
+ // the same url. We'll expose a call to 'reload' to allow a user to load
|
|
120
|
+ // the existing page.
|
|
121
|
+ if ([request.URL isEqual:_webView.request.URL]) {
|
|
122
|
+ return;
|
|
123
|
+ }
|
|
124
|
+ if (!request.URL) {
|
|
125
|
+ // Clear the webview
|
|
126
|
+ [_webView loadHTMLString:@"" baseURL:nil];
|
|
127
|
+ return;
|
|
128
|
+ }
|
|
129
|
+ [_webView loadRequest:request];
|
|
130
|
+ }
|
|
131
|
+}
|
|
132
|
+
|
|
133
|
+- (void)layoutSubviews
|
|
134
|
+{
|
|
135
|
+ [super layoutSubviews];
|
|
136
|
+ _webView.frame = self.bounds;
|
|
137
|
+}
|
|
138
|
+
|
|
139
|
+- (void)setContentInset:(UIEdgeInsets)contentInset
|
|
140
|
+{
|
|
141
|
+ _contentInset = contentInset;
|
|
142
|
+ [RCTView autoAdjustInsetsForView:self
|
|
143
|
+ withScrollView:_webView.scrollView
|
|
144
|
+ updateOffset:NO];
|
|
145
|
+}
|
|
146
|
+
|
|
147
|
+- (void)setScalesPageToFit:(BOOL)scalesPageToFit
|
|
148
|
+{
|
|
149
|
+ if (_webView.scalesPageToFit != scalesPageToFit) {
|
|
150
|
+ _webView.scalesPageToFit = scalesPageToFit;
|
|
151
|
+ [_webView reload];
|
|
152
|
+ }
|
|
153
|
+}
|
|
154
|
+
|
|
155
|
+- (BOOL)scalesPageToFit
|
|
156
|
+{
|
|
157
|
+ return _webView.scalesPageToFit;
|
|
158
|
+}
|
|
159
|
+
|
|
160
|
+- (void)setBackgroundColor:(UIColor *)backgroundColor
|
|
161
|
+{
|
|
162
|
+ CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
|
|
163
|
+ self.opaque = _webView.opaque = (alpha == 1.0);
|
|
164
|
+ _webView.backgroundColor = backgroundColor;
|
|
165
|
+}
|
|
166
|
+
|
|
167
|
+- (UIColor *)backgroundColor
|
|
168
|
+{
|
|
169
|
+ return _webView.backgroundColor;
|
|
170
|
+}
|
|
171
|
+
|
|
172
|
+- (NSMutableDictionary<NSString *, id> *)baseEvent
|
|
173
|
+{
|
|
174
|
+ NSMutableDictionary<NSString *, id> *event = [[NSMutableDictionary alloc] initWithDictionary:@{
|
|
175
|
+ @"url": _webView.request.URL.absoluteString ?: @"",
|
|
176
|
+ @"loading" : @(_webView.loading),
|
|
177
|
+ @"title": [_webView stringByEvaluatingJavaScriptFromString:@"document.title"],
|
|
178
|
+ @"canGoBack": @(_webView.canGoBack),
|
|
179
|
+ @"canGoForward" : @(_webView.canGoForward),
|
|
180
|
+ }];
|
|
181
|
+
|
|
182
|
+ return event;
|
|
183
|
+}
|
|
184
|
+
|
|
185
|
+- (void)refreshContentInset
|
|
186
|
+{
|
|
187
|
+ [RCTView autoAdjustInsetsForView:self
|
|
188
|
+ withScrollView:_webView.scrollView
|
|
189
|
+ updateOffset:YES];
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+#pragma mark - UIWebViewDelegate methods
|
|
193
|
+
|
|
194
|
+- (BOOL)webView:(__unused UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
|
|
195
|
+ navigationType:(UIWebViewNavigationType)navigationType
|
|
196
|
+{
|
|
197
|
+ BOOL isJSNavigation = [request.URL.scheme isEqualToString:RNCJSNavigationScheme];
|
|
198
|
+
|
|
199
|
+ static NSDictionary<NSNumber *, NSString *> *navigationTypes;
|
|
200
|
+ static dispatch_once_t onceToken;
|
|
201
|
+ dispatch_once(&onceToken, ^{
|
|
202
|
+ navigationTypes = @{
|
|
203
|
+ @(UIWebViewNavigationTypeLinkClicked): @"click",
|
|
204
|
+ @(UIWebViewNavigationTypeFormSubmitted): @"formsubmit",
|
|
205
|
+ @(UIWebViewNavigationTypeBackForward): @"backforward",
|
|
206
|
+ @(UIWebViewNavigationTypeReload): @"reload",
|
|
207
|
+ @(UIWebViewNavigationTypeFormResubmitted): @"formresubmit",
|
|
208
|
+ @(UIWebViewNavigationTypeOther): @"other",
|
|
209
|
+ };
|
|
210
|
+ });
|
|
211
|
+
|
|
212
|
+ // skip this for the JS Navigation handler
|
|
213
|
+ if (!isJSNavigation && _onShouldStartLoadWithRequest) {
|
|
214
|
+ NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
215
|
+ [event addEntriesFromDictionary: @{
|
|
216
|
+ @"url": (request.URL).absoluteString,
|
|
217
|
+ @"navigationType": navigationTypes[@(navigationType)]
|
|
218
|
+ }];
|
|
219
|
+ if (![self.delegate webView:self
|
|
220
|
+ shouldStartLoadForRequest:event
|
|
221
|
+ withCallback:_onShouldStartLoadWithRequest]) {
|
|
222
|
+ return NO;
|
|
223
|
+ }
|
|
224
|
+ }
|
|
225
|
+
|
|
226
|
+ if (_onLoadingStart) {
|
|
227
|
+ // We have this check to filter out iframe requests and whatnot
|
|
228
|
+ BOOL isTopFrame = [request.URL isEqual:request.mainDocumentURL];
|
|
229
|
+ if (isTopFrame) {
|
|
230
|
+ NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
231
|
+ [event addEntriesFromDictionary: @{
|
|
232
|
+ @"url": (request.URL).absoluteString,
|
|
233
|
+ @"navigationType": navigationTypes[@(navigationType)]
|
|
234
|
+ }];
|
|
235
|
+ _onLoadingStart(event);
|
|
236
|
+ }
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ if (isJSNavigation && [request.URL.host isEqualToString:kPostMessageHost]) {
|
|
240
|
+ NSString *data = request.URL.query;
|
|
241
|
+ data = [data stringByReplacingOccurrencesOfString:@"+" withString:@" "];
|
|
242
|
+ data = [data stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
243
|
+
|
|
244
|
+ NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
245
|
+ [event addEntriesFromDictionary: @{
|
|
246
|
+ @"data": data,
|
|
247
|
+ }];
|
|
248
|
+
|
|
249
|
+ NSString *source = @"document.dispatchEvent(new MessageEvent('message:received'));";
|
|
250
|
+
|
|
251
|
+ [_webView stringByEvaluatingJavaScriptFromString:source];
|
|
252
|
+
|
|
253
|
+ _onMessage(event);
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ // JS Navigation handler
|
|
257
|
+ return !isJSNavigation;
|
|
258
|
+}
|
|
259
|
+
|
|
260
|
+- (void)webView:(__unused UIWebView *)webView didFailLoadWithError:(NSError *)error
|
|
261
|
+{
|
|
262
|
+ if (_onLoadingError) {
|
|
263
|
+ if ([error.domain isEqualToString:NSURLErrorDomain] && error.code == NSURLErrorCancelled) {
|
|
264
|
+ // NSURLErrorCancelled is reported when a page has a redirect OR if you load
|
|
265
|
+ // a new URL in the WebView before the previous one came back. We can just
|
|
266
|
+ // ignore these since they aren't real errors.
|
|
267
|
+ // http://stackoverflow.com/questions/1024748/how-do-i-fix-nsurlerrordomain-error-999-in-iphone-3-0-os
|
|
268
|
+ return;
|
|
269
|
+ }
|
|
270
|
+
|
|
271
|
+ if ([error.domain isEqualToString:@"WebKitErrorDomain"] && error.code == 102) {
|
|
272
|
+ // Error code 102 "Frame load interrupted" is raised by the UIWebView if
|
|
273
|
+ // its delegate returns FALSE from webView:shouldStartLoadWithRequest:navigationType
|
|
274
|
+ // when the URL is from an http redirect. This is a common pattern when
|
|
275
|
+ // implementing OAuth with a WebView.
|
|
276
|
+ return;
|
|
277
|
+ }
|
|
278
|
+
|
|
279
|
+ NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
280
|
+ [event addEntriesFromDictionary:@{
|
|
281
|
+ @"domain": error.domain,
|
|
282
|
+ @"code": @(error.code),
|
|
283
|
+ @"description": error.localizedDescription,
|
|
284
|
+ }];
|
|
285
|
+ _onLoadingError(event);
|
|
286
|
+ }
|
|
287
|
+}
|
|
288
|
+
|
|
289
|
+- (void)webViewDidFinishLoad:(UIWebView *)webView
|
|
290
|
+{
|
|
291
|
+ if (_messagingEnabled) {
|
|
292
|
+ #if RCT_DEV
|
|
293
|
+ // See isNative in lodash
|
|
294
|
+ NSString *testPostMessageNative = @"String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage')";
|
|
295
|
+ BOOL postMessageIsNative = [
|
|
296
|
+ [webView stringByEvaluatingJavaScriptFromString:testPostMessageNative]
|
|
297
|
+ isEqualToString:@"true"
|
|
298
|
+ ];
|
|
299
|
+ if (!postMessageIsNative) {
|
|
300
|
+ RCTLogError(@"Setting onMessage on a WebView overrides existing values of window.postMessage, but a previous value was defined");
|
|
301
|
+ }
|
|
302
|
+ #endif
|
|
303
|
+ NSString *source = [NSString stringWithFormat:
|
|
304
|
+ @"(function() {"
|
|
305
|
+ "window.originalPostMessage = window.postMessage;"
|
|
306
|
+
|
|
307
|
+ "var messageQueue = [];"
|
|
308
|
+ "var messagePending = false;"
|
|
309
|
+
|
|
310
|
+ "function processQueue() {"
|
|
311
|
+ "if (!messageQueue.length || messagePending) return;"
|
|
312
|
+ "messagePending = true;"
|
|
313
|
+ "window.location = '%@://%@?' + encodeURIComponent(messageQueue.shift());"
|
|
314
|
+ "}"
|
|
315
|
+
|
|
316
|
+ "window.postMessage = function(data) {"
|
|
317
|
+ "messageQueue.push(String(data));"
|
|
318
|
+ "processQueue();"
|
|
319
|
+ "};"
|
|
320
|
+
|
|
321
|
+ "document.addEventListener('message:received', function(e) {"
|
|
322
|
+ "messagePending = false;"
|
|
323
|
+ "processQueue();"
|
|
324
|
+ "});"
|
|
325
|
+ "})();", RNCJSNavigationScheme, kPostMessageHost
|
|
326
|
+ ];
|
|
327
|
+ [webView stringByEvaluatingJavaScriptFromString:source];
|
|
328
|
+ }
|
|
329
|
+ if (_injectedJavaScript != nil) {
|
|
330
|
+ NSString *jsEvaluationValue = [webView stringByEvaluatingJavaScriptFromString:_injectedJavaScript];
|
|
331
|
+
|
|
332
|
+ NSMutableDictionary<NSString *, id> *event = [self baseEvent];
|
|
333
|
+ event[@"jsEvaluationValue"] = jsEvaluationValue;
|
|
334
|
+
|
|
335
|
+ _onLoadingFinish(event);
|
|
336
|
+ }
|
|
337
|
+ // we only need the final 'finishLoad' call so only fire the event when we're actually done loading.
|
|
338
|
+ else if (_onLoadingFinish && !webView.loading && ![webView.request.URL.absoluteString isEqualToString:@"about:blank"]) {
|
|
339
|
+ _onLoadingFinish([self baseEvent]);
|
|
340
|
+ }
|
|
341
|
+}
|
|
342
|
+
|
|
343
|
+@end
|