我将所有html嵌入到WKWebView中,一直有效,直到我认识到WKWebView无法在本地加载xml文件
$.ajax({
type: "GET",
url: "tags.xml",
dataType: "xml",
cache: false,
success: function(xml) {
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
我的UIWebView代码
//urlFolder is located locally in a temporary file: tmp/www/htmlFolder
//urlFile is located in the urlFolder: tmp/www/htmlFolder/index.html
//xml file is located in the urlFolder: tmp/www/htmlFolder/tags.xml
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
_webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
[_webView loadFileURL:urlFile allowingReadAccessToURL:urlFolder];
[self.view addSubview:_webView];
注意:我使用的是XCode7.1 Beta,Objective-C,ios9.1,WKWebView
2 回答
根据我的发现,他们在WKWebViews中禁用了跨源请求 .
搜索cors或xhr WKWebView以获取有关此问题的更多信息 . 我认为它必定是某种类型的错误,因为在使用本地文件的“普通”UIWebViews中总是可能的(例如你的例子) .
但是你可以在你的应用程序中运行small/lightweight http-server,这对我很有用 . 确保在.host文件的.plist文件中为
App Transport Security Settings
添加例外 .您可以使用我从一些代码示例拼凑而成的插件而不是本地服务器 . 它应该通过XHR为您启用本地文件访问 . 我用它来恢复对我们应用程序上的大量本地文件的访问 .
https://github.com/TheMattRay/cordova-plugin-wkwebviewxhrfix