【问题标题】:React Native WebView Cross origin requests are only supported for protocol schemes: http, data, chrome, httpsReact Native WebView 跨域请求仅支持协议方案:http、data、chrome、https
【发布时间】:2018-10-10 10:03:00
【问题描述】:

我们正在尝试在 Android React Native WebView 中使用 Angular JS 和 HTMl 加载 SPA 应用。该解决方案在开发服务器上运行良好,但是当我们尝试在调试和发布版本中加载它时,它会引发以下错误。我们在 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);
}

按照here 的说明尝试了解决方案,但仍然无法加载所需的 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

【问题讨论】:

  • 如果有人来这里,这是对我有用的代码 allowUniversalAccessFromFileURLs= {true} for IOS in latest

标签: javascript android react-native webview react-native-android


【解决方案1】:

按照核心团队成员的建议关闭问题。 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 中运行没有任何问题。

【讨论】:

  • 我已经尝试过这个及其在 android 上的工作。在IOS上怎么样?你有什么解决办法吗?谢谢
  • 这里也一样,在 Android 中可以正常工作,但在 iOS 中却不行,stackoverflow.com/questions/61523598/…
猜你喜欢
  • 2017-10-11
  • 2017-12-25
  • 2016-07-27
  • 1970-01-01
  • 2018-04-16
  • 2020-07-02
  • 2015-12-11
  • 2015-11-17
相关资源
最近更新 更多