衆所周知,React-Native原生的Webview的屬性onShouldStartLoadWithRequest僅支持IOS平臺,可是每每在開發中兩端都須要進行URl的攔截,進行對應的業務須要。 react
經過不斷學習,在https://github.com/react-native-community/react-native-webview的基礎上,整理出來一個方案,而且發佈到了npm上,小夥伴們有須要的能夠參考一下。 github.com/wayne214/re….使用方式以下: 1.添加依賴包git
yarn add react-native-webview-crossplatform react-native link react-native-webview-crossplatformgithub
2.在須要的業務頁面導入web
import { WebView } from 'react-native-webview-crossplatform'
export default class webview extends Component<Props> {
constructor(props){
super(props);
}
render() {
return (
<View style={styles.container}>
<WebView
source={{ uri: 'https://infinite.red/react-native' }}
style={{ marginTop: 20 }}
onLoadProgress={e=>console.log(e.nativeEvent.progress)}
onShouldStartLoadWithRequest={(e)=> {
console.log('攔截', e)
return true
}}
renderError={()=> {
return <View>
<Text>我是錯誤頁面</Text>
</View>
}}
/>
</View>
);
}
}
複製代碼