Search code examples
androidhtmlreact-nativewebviewreact-native-webview

Android : script in HTML can't call addEventListener when I setting shouldOverUriLoading return false


When I using react-native-webview, I have to set function shouldOverideUrlLoading return false. So that my script in HTML can't listen to the message I send from my Component. It worked fine in iOS but Android my function addEventListener in HTML doesn't trigger anything

My HTML javascript looks like:

console.log("checkme; data", data);
            if (data.data) {
                alert("checkMe:" + JSON.stringify(data.data));
                symbol = data.data;
            }
            // initOnReady();
        })

In my Component I fire postMessage to my WebView like this: this.WebView.postMessage("hello");

In iOS, nothing wrong, but in Android, maybe shouldOverideUrlLoading stopping me to pass data from component to webview to update HTML file. So do you have another solution to pass data from component to webview, not by postMessage or I'm wrong in something? Please help me out


Solution

  • I have to customize react-native-webview to resolve this issue. This is function I have to modify in RNCWebviewManager.java

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      String FILE_LOCAL_PATH = "file:///";
      if (url.contains(FILE_LOCAL_PATH)){ // kyun: Fix allow another HTML file load through 1 first HTML 
        return false;
      }
      dispatchEvent(
        view,
        new TopShouldStartLoadWithRequestEvent(
          view.getId(),
          createWebViewEvent(view, url)));
      return true;
    }