【问题标题】:React native webview files are not loading in ios device, but loading fine in simulatorReact 本机 webview 文件未在 ios 设备中加载,但在模拟器中加载正常
【发布时间】:2023-12-18 05:15:01
【问题描述】:

我将本地文件存储在xcode中,文件在模拟器中加载但在ios设备中没有加载

我使用 react-native-webview 5.0.1 版,在我的 webview 中我将源传递为 {uri: 'ICF-Package/ICFPackage/index.html'}

<WebView
                source={Platform.OS === 'ios' ? 
                {uri: 'ICF-Package/ICFPackage/index.html'} :
                { uri: 'file:///android_asset/ICF-Package/ICFPackage/index.html' }
              }
                    ref={(webView) => this.webView = webView}
                    originWhitelist={'["*"]'}
                    javaScriptEnabled={true}
                    domStorageEnabled={true}
                    startInLoadingState={true}
                    useWebKit = {true}
                    //scalesPageToFit={true}
                    scrollEnabled={false}
                    onLoad={() => this.sendPostMessage()}
                    allowFileAccess={true}
                    allowUniversalAccessFromFileURLs={true}
                    allowFileAccessFromFileURLs={true}
                    allowsInlineMediaPlayback={true}
                    mediaPlaybackRequiresUserAction={true}
                 />

我没有收到任何错误消息,但文件没有在真实设备中加载

【问题讨论】:

    标签: react-native


    【解决方案1】:

    如果在您的设备上运行,也许可以帮助您更深入地调试 WebView:https://*.com/a/48572797/1256697

    但作为本地文件的小解决方法,您可以尝试使用 'html' 参数而不是 IOS 的 'uri' 参数:

    const myHTML = require('./ICF-Package/ICFPackage/index.html');
    <WebView
      source={PolicyHTML}
      style={{flex: 1}}
     />
    

    另见:https://aboutreact.com/load-local-html-file-url-using-react-native-webview/

    但也许为 IOS 解决这个问题的最干净的方法是将你的 html 文件的副本放在 Project &gt; resources &gt; index.html 中,然后像这样将它与你的 Webview 链接:

    <WebView
      style={{flex: 1}}
      originWhitelist={['*']}
      source={'./resources/index.html'}
      javaScriptEnabled={true}
      domStorageEnabled={true}
    />
    

    【讨论】:

      最近更新 更多