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