【发布时间】: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