RN WebVIew组件onShouldStartLoadWithRequest方法仅ios有效,android怎么处理?

解决方案

使用RN文档中推荐的 react-native-webview 库,版本在5.0.8版本及之后

背景

遇到一个需求,拦截h5的协议,然后调用底层方法,项目是用的RN。

搜索得知,RN WebView组件的onShouldStartLoadWithRequest方法可以拦截,但文档中只有ios可用。

然后又搜了半天,Android怎么弄,没搜到,好不容易看到一篇文章的评论中说,新的WebView的onShouldStartLoadWithRequest在android中也能正常使用,我仔细看了看,发现他说的WebView是RN文档中推荐使用的react-native-webview,然后我就去看了react-native-webview仓库中的api说明,发现android真的支持onShouldStartLoadWithRequest方法。

说明

react-native-webview v5.0.7版本及之前

react-native-webview/Reference.md at v5.0.7 · react-native-webview/react-native-webview · GitHub

react-native-webview v5.0.8版本及之后

https://github.com/react-native-webview/react-native-webview/blob/v5.0.8/docs/Reference.md#onshouldstartloadwithrequest

注意

Android在第一次加载不会调用

我的理解是用WebView打开h5时,此方法不会触发

 通过日志,刚打开h5时,只打印了onNavigationStateChange,然后我在h5中点击按钮触发下面的代码时,才打印onShouldStartLoadWithRequest日志

let url = 'openapp://functionId'
window.location.href = url

猜你喜欢

转载自blog.csdn.net/weixin_41786574/article/details/123209223
RN