Cordova 6 app无法从file://路径加载

Ade*_*Ade 5 cors ios cordova wkwebview

我已将现有应用更新到Cordova 6.0.0,并且在使用XCode 7.2.1定位iOS 9的iOS设备上运行它时遇到问题.

我已经安装了WKWebView插件: cordova-plugin-wkwebview-engine 1.0.2

运行使用AngularJS构建的应用程序时,它尝试做的第一件事就是打开一个json文件:

$http.get('data/config.json').success(function(data) {
  // do stuff
});
Run Code Online (Sandbox Code Playgroud)

但是我们在Web视图控制台中收到此错误:

XMLHttpRequest cannot load file:///var/mobile/Containers/Bundle/Application/
E9D74C94-ADC6-410F-9F41-7CE63CB7877F/Milk.app/www/data/config.json. Cross 
origin requests are only supported for HTTP.
Run Code Online (Sandbox Code Playgroud)

在config.xml文件中,我们有:

<access origin="*" subdomains="true" />
Run Code Online (Sandbox Code Playgroud)

为什么file://请求被阻止,我该如何解决?

*编辑*

根据插件页面:

"在iOS 9中,Apple修复了iOS 8中存在的问题,您无法使用file://加载语言环境文件,并且必须使用本地Web服务器.但是,您仍然无法使用文件中的XHR:/ /在服务器上未启用CORS的协议."

"你的服务器"是什么意思?什么服务器?我们正在加载本地文件,没有服务器!

小智 9

将这些行添加到您的 config.xml 文件中。这解决了我读取本地文件的问题。

<platform name="ios">
    <preference name="scheme" value="app" />
    <preference name="hostname" value="localhost" />
</platform>
Run Code Online (Sandbox Code Playgroud)


jce*_*ile 6

编辑:

离子团队一直在研究WKWebViewEngine插件的一个分支,并修复了一些XHR问题,不确定这个问题是否已修复.你可以看看https://github.com/driftyco/cordova-plugin-wkwebview-engine

旧答案:这是一个已知问题,不允许使用本地file://网址(https://issues.apache.org/jira/browse/CB-10143)XmlHttpRequestsWKWebViewEngine

有关更多已知问题,请参阅https://issues.apache.org/jira/browse/CB-10237?jql=labels%20%3D%20wkwebview-known-issues

如果你想让本地ajax调用工作你应该使用wkwebview-engine-localhost插件(https://github.com/apache/cordova-plugins/tree/master/wkwebview-engine-localhost)

或Telerik的WKWebView插件(https://github.com/Telerik-Verified-Plugins/WKWebView).

它们都将使用本地Web服务器来解决一些已知的WKWebView限制.