繁体   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