簡體   English   中英

僅協議方案支持React Native WebView Cross起源請求:http,data,chrome,https

[英]React Native WebView Cross origin requests are only supported for protocol schemes: http, data, chrome, https

我們正在嘗試在Android React Native WebView中使用Angular JS和HTMl加載SPA應用程序。 該解決方案在Dev服務器上運行良好,但是當我們嘗試在調試中加載它並發布版本時,它會引發以下錯誤。 我們已經在chrome中對其進行了調試,發現除html文件外,所有資源都正在加載。 僅Index.html文件正在加載。

控制台錯誤:

Failed to load file:///android_asset/template.html: 
Cross origin requests are only supported for protocol schemes: http, data, chrome, https.

我們正在RN WebView中加載本地html文件:

<WebView 
  ref="webview"
  scalesPageToFit={true} 
  source={{uri: 'file:///android_asset/www/index.html'}}
  javaScriptEnabled={true}
  domStorageEnabled={true}
  nativeConfig={{props: {webContentsDebuggingEnabled: true}}}
  setAllowFileAccessFromFileURLs={true}
  setAllowUniversalAccessFromFileURLs={true}
 /> 

我們試圖在此路徑下更改ReactWebViewManager.java

我們對MyReactProjectName \\ node_modules \\ react-native \\ ReactAndroid \\ src \\ main \\ java \\ com \\ facebook \\ react \\ views \\ webview \\ ReactWebViewManager.java進行了以下更改:

settings.setDomStorageEnabled(true);

settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  settings.setAllowFileAccessFromFileURLs(true);
  setAllowUniversalAccessFromFileURLs(webView, true);
}

試圖解決的解釋在這里 ,仍然無法加載所需的HTML文件。

有什么方法可以更改ReactWebViewManager.java來訪問本地文件,因為RN WebView無法找到本地HTML文件並拋出CORS錯誤?

環境

React Native Environment Info:

System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i3 CPU 540 @ 3.07GHz
Memory: 125.40 MB / 12.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 9.5.0 - /usr/local/bin/node
Yarn: 1.10.1 - ~/.yarn/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0
IDEs:
Android Studio: 3.0 AI-171.4443003
Xcode: 10.0/10A255 - /usr/bin/xcodebuild
npmPackages:
react: 16.5.0 => 16.5.0
react-native: 0.57.1 => 0.57.1
npmGlobalPackages:
create-react-native-app: 1.0.0
react-native-cli: 2.0.1
react-native-git-upgrade: 0.2.7

根據核心團隊成員的建議解決問題。 WebView已移至https://github.com/react-native-community/react-native-webview

$ yarn add react-native-webview
$ react-native link react-native-webview

react-native-webview導入WebView組件,如下所示使用它:

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { WebView } from 'react-native-webview';

// ...
class MyWebComponent extends Component {
  render() {
    return (
      <WebView
        source={{ uri: 'https://infinite.red/react-native' }}
        style={{ marginTop: 20 }}
      />
    );
  }
}

以上內容可在Android中正常使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM