|
@@ -13,7 +13,7 @@
|
13
|
13
|
|
14
|
14
|
#import "objc/runtime.h"
|
15
|
15
|
|
16
|
|
-static NSString *const MessageHanderName = @"ReactNative";
|
|
16
|
+static NSString *const MessageHandlerName = @"ReactNativeWebview";
|
17
|
17
|
|
18
|
18
|
// runtime trick to remove WKWebView keyboard default toolbar
|
19
|
19
|
// see: http://stackoverflow.com/questions/19033292/ios-7-uiwebview-keyboard-issue/19042279#19042279
|
|
@@ -101,7 +101,22 @@ static NSString *const MessageHanderName = @"ReactNative";
|
101
|
101
|
wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool];
|
102
|
102
|
}
|
103
|
103
|
wkWebViewConfig.userContentController = [WKUserContentController new];
|
104
|
|
- [wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
|
|
104
|
+
|
|
105
|
+ if (_messagingEnabled) {
|
|
106
|
+ [wkWebViewConfig.userContentController addScriptMessageHandler:self name:MessageHandlerName];
|
|
107
|
+
|
|
108
|
+ NSString *source = [NSString stringWithFormat:
|
|
109
|
+ @"window.%@ = {"
|
|
110
|
+ " postMessage: function (data) {"
|
|
111
|
+ " window.webkit.messageHandlers.%@.postMessage(String(data));"
|
|
112
|
+ " }"
|
|
113
|
+ "};", MessageHandlerName, MessageHandlerName
|
|
114
|
+ ];
|
|
115
|
+
|
|
116
|
+ WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
|
|
117
|
+ [wkWebViewConfig.userContentController addUserScript:script];
|
|
118
|
+ }
|
|
119
|
+
|
105
|
120
|
wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
|
106
|
121
|
#if WEBKIT_IOS_10_APIS_AVAILABLE
|
107
|
122
|
wkWebViewConfig.mediaTypesRequiringUserActionForPlayback = _mediaPlaybackRequiresUserAction
|
|
@@ -148,7 +163,7 @@ static NSString *const MessageHanderName = @"ReactNative";
|
148
|
163
|
- (void)removeFromSuperview
|
149
|
164
|
{
|
150
|
165
|
if (_webView) {
|
151
|
|
- [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHanderName];
|
|
166
|
+ [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHandlerName];
|
152
|
167
|
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
|
153
|
168
|
[_webView removeFromSuperview];
|
154
|
169
|
_webView = nil;
|
|
@@ -184,7 +199,7 @@ static NSString *const MessageHanderName = @"ReactNative";
|
184
|
199
|
|
185
|
200
|
/**
|
186
|
201
|
* This method is called whenever JavaScript running within the web view calls:
|
187
|
|
- * - window.webkit.messageHandlers.[MessageHanderName].postMessage
|
|
202
|
+ * - window.webkit.messageHandlers[MessageHandlerName].postMessage
|
188
|
203
|
*/
|
189
|
204
|
- (void)userContentController:(WKUserContentController *)userContentController
|
190
|
205
|
didReceiveScriptMessage:(WKScriptMessage *)message
|
|
@@ -253,7 +268,6 @@ static NSString *const MessageHanderName = @"ReactNative";
|
253
|
268
|
|
254
|
269
|
-(void)setHideKeyboardAccessoryView:(BOOL)hideKeyboardAccessoryView
|
255
|
270
|
{
|
256
|
|
-
|
257
|
271
|
if (_webView == nil) {
|
258
|
272
|
_savedHideKeyboardAccessoryView = hideKeyboardAccessoryView;
|
259
|
273
|
return;
|
|
@@ -264,6 +278,7 @@ static NSString *const MessageHanderName = @"ReactNative";
|
264
|
278
|
}
|
265
|
279
|
|
266
|
280
|
UIView* subview;
|
|
281
|
+
|
267
|
282
|
for (UIView* view in _webView.scrollView.subviews) {
|
268
|
283
|
if([[view.class description] hasPrefix:@"WK"])
|
269
|
284
|
subview = view;
|
|
@@ -303,10 +318,10 @@ static NSString *const MessageHanderName = @"ReactNative";
|
303
|
318
|
{
|
304
|
319
|
NSDictionary *eventInitDict = @{@"data": message};
|
305
|
320
|
NSString *source = [NSString
|
306
|
|
- stringWithFormat:@"document.dispatchEvent(new MessageEvent('message', %@));",
|
|
321
|
+ stringWithFormat:@"window.dispatchEvent(new MessageEvent('message', %@));",
|
307
|
322
|
RCTJSONStringify(eventInitDict, NULL)
|
308
|
323
|
];
|
309
|
|
- [self evaluateJS: source thenCall: nil];
|
|
324
|
+ [self injectJavaScript: source];
|
310
|
325
|
}
|
311
|
326
|
|
312
|
327
|
- (void)layoutSubviews
|
|
@@ -520,7 +535,6 @@ static NSString *const MessageHanderName = @"ReactNative";
|
520
|
535
|
}];
|
521
|
536
|
}
|
522
|
537
|
|
523
|
|
-
|
524
|
538
|
/**
|
525
|
539
|
* Called when the navigation is complete.
|
526
|
540
|
* @see https://fburl.com/rtys6jlb
|
|
@@ -528,35 +542,11 @@ static NSString *const MessageHanderName = @"ReactNative";
|
528
|
542
|
- (void) webView:(WKWebView *)webView
|
529
|
543
|
didFinishNavigation:(WKNavigation *)navigation
|
530
|
544
|
{
|
531
|
|
- if (_messagingEnabled) {
|
532
|
|
- #if RCT_DEV
|
533
|
|
-
|
534
|
|
- // Implementation inspired by Lodash.isNative.
|
535
|
|
- NSString *isPostMessageNative = @"String(String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage'))";
|
536
|
|
- [self evaluateJS: isPostMessageNative thenCall: ^(NSString *result) {
|
537
|
|
- if (! [result isEqualToString:@"true"]) {
|
538
|
|
- RCTLogError(@"Setting onMessage on a WebView overrides existing values of window.postMessage, but a previous value was defined");
|
539
|
|
- }
|
540
|
|
- }];
|
541
|
|
- #endif
|
542
|
|
-
|
543
|
|
- NSString *source = [NSString stringWithFormat:
|
544
|
|
- @"(function() {"
|
545
|
|
- "window.originalPostMessage = window.postMessage;"
|
546
|
|
-
|
547
|
|
- "window.postMessage = function(data) {"
|
548
|
|
- "window.webkit.messageHandlers.%@.postMessage(String(data));"
|
549
|
|
- "};"
|
550
|
|
- "})();",
|
551
|
|
- MessageHanderName
|
552
|
|
- ];
|
553
|
|
- [self evaluateJS: source thenCall: nil];
|
554
|
|
- }
|
555
|
|
-
|
556
|
545
|
if (_injectedJavaScript) {
|
557
|
546
|
[self evaluateJS: _injectedJavaScript thenCall: ^(NSString *jsEvaluationValue) {
|
558
|
547
|
NSMutableDictionary *event = [self baseEvent];
|
559
|
548
|
event[@"jsEvaluationValue"] = jsEvaluationValue;
|
|
549
|
+
|
560
|
550
|
if (self.onLoadingFinish) {
|
561
|
551
|
self.onLoadingFinish(event);
|
562
|
552
|
}
|