Просмотр исходного кода

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-bf220e0adf.png)
Ryan Linton 6 лет назад
Родитель
Сommit
ec469cf00d
2 измененных файлов: 7 добавлений и 3 удалений
  1. 1
    1
      docs/Reference.md
  2. 6
    2
      ios/RNCWKWebView.m

+ 1
- 1
docs/Reference.md Просмотреть файл

@@ -102,7 +102,7 @@ Controls whether to adjust the content inset for web views that are placed behin
102 102
 
103 103
 ### `injectedJavaScript`
104 104
 
105
-Set this to provide JavaScript that will be injected into the web page when the view loads.
105
+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.
106 106
 
107 107
 | Type   | Required |
108 108
 | ------ | -------- |

+ 6
- 2
ios/RNCWKWebView.m Просмотреть файл

@@ -512,8 +512,12 @@ static NSString *const MessageHanderName = @"ReactNative";
512 512
           thenCall: (void (^)(NSString*)) callback
513 513
 {
514 514
   [self.webView evaluateJavaScript: js completionHandler: ^(id result, NSError *error) {
515
-    if (error == nil && callback != nil) {
516
-      callback([NSString stringWithFormat:@"%@", result]);
515
+    if (error == nil) {
516
+      if (callback != nil) {
517
+        callback([NSString stringWithFormat:@"%@", result]);
518
+      }
519
+    } else {
520
+      RCTLogError(@"Error evaluating injectedJavaScript: This is possibly due to an unsupported return type. Try adding true to the end of your injectedJavaScript string.");
517 521
     }
518 522
   }];
519 523
 }