Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent WebView from loading URL in Android (React Native)

Currently, OnShouldStartLoadWithRequest is only supported on iOS. A PR to add support for this on Android was closed. If a WebView on Android tries to open a custom URL scheme, the app will crash -- which is causing my app to crash as well.

The app I'm working on needs to intercept a WebView attempting to open a custom URL scheme and cancel it before opening that URL (which, on iOS, returning false on OnShouldStartLoadWithRequest is a great way to do that).

What's the best way to do this on Android?

I want this code to work:

        <WebView
            ref={WEBVIEW_REF} 
            source={{uri: INITIAL_URI}}
            onShouldStartLoadWithRequest={this.onShouldStartLoadWithRequest}
            style={{width, height: height - navBarHeight, backgroundColor: "#fff"}}
        />

 onShouldStartLoadWithRequest(navigator) {
    if (navigator.url.indexOf(INTERCEPT_URL) === -1) {
        return true;
    }    
    return false;
  }

I'm personally okay with pretty much anything that works -- including modifying ReactWebViewManager.java manually. I just need to get this out the door.

like image 722
Jarred Sumner Avatar asked Sep 25 '16 01:09

Jarred Sumner


People also ask

How do I stop WebView loading in react-native?

You don't need to stop loading the webview, it will stop loading when it's finished loading the web page you're on.

Can we use WebView in react-native?

WebViews offer developers opportunities to render any web components in a React Native application. A web component can be anything from a whole webpage/application or just a simple HTML file. The package react-native-webview makes it super simple to embed WebViews into your React Native apps!

How do you refresh a WebView in react-native?

The solution to How To Reload Webview In React Native will be demonstrated using examples in this article. You can set a key to the webview key={this. state. key} and then you can reload it by updating the state this.


1 Answers

You can use the onNavigationStateChange property, which will get the same navigator object as you get in your function onShouldStartLoadWithRequest.

 <WebView
        ref={WEBVIEW_REF} 
        source={{uri: INITIAL_URI}}
        onShouldStartLoadWithRequest={this.onShouldStartLoadWithRequest} //for iOS
        onNavigationStateChange ={this.onShouldStartLoadWithRequest} //for Android

        style={{width, height: height - navBarHeight, backgroundColor: "#fff"}}
    />

And then in the function

onShouldStartLoadWithRequest(navigator) {
    if (navigator.url.indexOf(INTERCEPT_URL) === -1) {
        return true;
    } else {
        this.refs[WEBVIEW_REF].stopLoading(); //Some reference to your WebView to make it stop loading that URL
        return false;
    }    
}

I had the same requirement as you, and that's how've I solved it.

like image 113
maha_funk Avatar answered Sep 18 '22 13:09

maha_funk