From ec469cf00d72920488d27bdf17af2c6f1fdff56b Mon Sep 17 00:00:00 2001 From: Ryan Linton Date: Fri, 14 Dec 2018 02:24:52 -0800 Subject: [PATCH] fix(WKWebview): Surface evaluateJavaScript errors (#179) In the current code using `startInLoadingState` and `injectedJavaScript` will result in an infinite loading state if `injectedJavaScript` fails to evaluate for some reason. This adds a red box error explaining there was a failure to evaluate javascript. In my case this was do to the JS string not returning a valid type so I've added a that as a potential solution in the error message and added some documentation to the API Reference with some additional warnings. To reproduce the existing behavior setup a webview with `startInLoadingState` and `injectedJavaScript` that returns an invalid type (in my case it returned a function). You should see an infinite loading state as `onLoadEnd` is never called. Try the same with this branch and you'll get a nice red box error suggesting one potential solution to the problem. ![simulator screen shot - iphone 8 plus - 2018-11-28 at 15 09 25](https://user-images.githubusercontent.com/1944151/49193714-fccde100-f334-11e8-89dc-bf220e0adf23.png) --- docs/Reference.md | 2 +- ios/RNCWKWebView.m | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/Reference.md b/docs/Reference.md index 7d3752f..9257aca 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -102,7 +102,7 @@ Controls whether to adjust the content inset for web views that are placed behin ### `injectedJavaScript` -Set this to provide JavaScript that will be injected into the web page when the view loads. +Set this to provide JavaScript that will be injected into the web page when the view loads. Make sure the string evaluates to a valid type (`true` works) and doesn't otherwise throw an exception. | Type | Required | | ------ | -------- | diff --git a/ios/RNCWKWebView.m b/ios/RNCWKWebView.m index 95ce13c..f386051 100644 --- a/ios/RNCWKWebView.m +++ b/ios/RNCWKWebView.m @@ -512,8 +512,12 @@ static NSString *const MessageHanderName = @"ReactNative"; thenCall: (void (^)(NSString*)) callback { [self.webView evaluateJavaScript: js completionHandler: ^(id result, NSError *error) { - if (error == nil && callback != nil) { - callback([NSString stringWithFormat:@"%@", result]); + if (error == nil) { + if (callback != nil) { + callback([NSString stringWithFormat:@"%@", result]); + } + } else { + RCTLogError(@"Error evaluating injectedJavaScript: This is possibly due to an unsupported return type. Try adding true to the end of your injectedJavaScript string."); } }]; }