react-native-webview-crossplatform支持iOS及Android拦截onShouldStartLoadWithRequest方法

众所周知,React-Native原生的Webview的属性onShouldStartLoadWithRequest仅支持IOS平台,可是每每在开发中两端都须要进行URl的拦截,进行对应的业务须要。 react

webview.png
经过不断学习,在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>
    );
  }
}
复制代码
相关文章
相关标签/搜索