reactnative

react-native-webview加载本地H5

2020-08-26  本文已影响0人  豆汁儿还是豆花儿

webview使用uri属性加载html资源。
1.简单且常变动的需求可以选择加载远程网页地址
2.当需要接入的网络资源很多,如一些独立的web应用,可以考虑本地接入

一.RN中创建静态资源目录

静态资源文件以.bundle结尾原因是:ios对以.bundle结尾的文件在打包后能够保持内部的资源引用路径

二.ios工程静态资源引入

三.Android资源路径设置

android { 
    ... 
    sourceSets { 
        main {
        asset.srcDirs = ['src/main/assets', '../../Static.bundle']
    } 
    } 
}

四.WebView访问本地HTML

let source = (Platform.OS === 'android' ? 'file:///android_asset/' : '') + `Static.bundle/index.html`;

<WebView
    ref={ref => (this.webViewRef = ref)}
    onMessage={this.onMessage}
    originWhitelist={['*']}
    allowFileAccess={true}
    source={{uri: source}}
    javaScriptEnabled={true}
    decelerationRate='normal'
    scrollEnabled={true}
    useWebKit={true}
    mediaPlaybackRequiresUserAction={true}
    mixedContentMode="compatibility"
    allowingReadAccessToURL="*"
    // onLoadEnd={() => this.setState({ loading: false })}
/>

接入本地HTML已大致完成, 可在iOS和Android平台测试

上一篇下一篇

猜你喜欢

热点阅读