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.
You don't need to stop loading the webview, it will stop loading when it's finished loading the web page you're on.
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With