React Native iOS Webview如何通过uri加载本地HTML

时间:2019-04-01 18:14:45

标签: javascript google-maps react-native webview react-native-ios

我正在尝试在webview中运行本地html文件,但无法加载全部内容。
我在具有相同html文件的Native应用程序上对其进行了测试,它显示得很好。

我不确定是什么问题。

本地代码

@IBOutlet weak var mapView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.uiDelegate = self;
        mapView.navigationDelegate = self;
        let url = Bundle.main.url(forResource: "index", withExtension: "html")!
        mapView.loadFileURL(url, allowingReadAccessTo: url)
        let request = URLRequest(url: url)
        mapView.load(request)
    }

反应本机代码

<WebView
          originWhitelist={['*']}
          onLoad={this.onLoad}
          source={Platform.OS === 'android' ? { uri: 'file:///android_asset/index.html' } : require('../ios/web/index.html')}
          dataDetectorTypes={'none'}
          ref='webview'
          onMessage={e => this.onMessage(JSON.parse(e.nativeEvent.data))}
          onShouldStartLoadWithRequest={this.onShouldStartLoadWithRequest}
          onNavigationStateChange={this.onShouldStartLoadWithRequest}
          injectedJavaScript={patchPostMessageJsCode}
          useWebKit={true}
          />

反应本机屏幕截图

enter image description here

本地屏幕截图

enter image description here

请帮助我。

1 个答案:

答案 0 :(得分:0)

尝试调整网络视图的大小,

import{Dimensions, SafeAreaView, WebView} from 'react-native';

<SafeAreaView style={{flex:1}}>

     <WebView
          originWhitelist={['*']}
          onLoad={this.onLoad}
          source={Platform.OS === 'android' ? { uri: 'file:///android_asset/index.html' } : require('../ios/web/index.html')}
          dataDetectorTypes={'none'}
          ref='webview'
          onMessage={e => this.onMessage(JSON.parse(e.nativeEvent.data))}
          onShouldStartLoadWithRequest={this.onShouldStartLoadWithRequest}
          onNavigationStateChange={this.onShouldStartLoadWithRequest}
          injectedJavaScript={patchPostMessageJsCode}
          useWebKit={true}
          style={{ height: Dimensions.get('window').width.height, width:Dimensions.get('window') , resizeMode: 'cover', flex: 1 }}
          />

</SafeAreaView>