【问题标题】:React Native iOS webview how to load local html by uriReact Native iOS webview如何通过uri加载本地html
【发布时间】:2019-04-01 18:14:45
【问题描述】:

我正在尝试在 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}
          />

React Native 截图

原生截图

请帮帮我。

【问题讨论】:

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


    【解决方案1】:

    尝试调整 webview 的大小,

    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>
    

    【讨论】:

    • 感谢您的回复。你认为调整 webview 的大小可以解决问题吗?
    • 我不太确定。即使调整大小后内容仍然无法完全加载?
    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多