7

我将所有 html 嵌入到 WKWebView 中,直到我认识到 WKWebView 无法在本地加载 xml 文件之前一切正常

$.ajax({
        类型:“获取”,
        网址:“tags.xml”,
        数据类型:“xml”,
        缓存:假,
        成功:函数(xml){

        },
        错误:函数(){

            alert("处理 XML 文件时发生错误。");
        }
    });

我的 UIWebView 代码

//urlFolder 位于本地一个临时文件中:tmp/www/htmlFolder
//urlFile位于urlFolder:tmp/www/htmlFolder/index.html
//xml文件位于urlFolder:tmp/www/htmlFolder/tags.xml


WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
    [_webView loadFileURL:urlFile allowedReadAccessToURL:urlFolder];

    [self.view addSubview:_webView];

注意:我使用的是 XCode7.1 Beta、Objective-C、ios9.1、WKWebView

4

2 回答 2

3

据我所知,他们在 WKWebViews 中禁用了跨域请求。

搜索 cors 或 xhr + WKWebView 以获取有关此问题的更多信息。我认为这一定是某种错误,因为在使用本地文件(如您的示例)的“普通”UIWebViews 中,这一直是可能的。

但是,您可以在您的应用程序中运行一个小型/轻量级的 http-server,这对我来说非常有用。确保App Transport Security Settings在 .plist 文件中为 localhost 添加一个例外。

于 2015-11-13T12:22:06.640 回答
0

您可以使用我从一些代码示例拼凑而成的插件,而不是本地服务器。它应该为您启用通过 XHR 的本地文件访问。我用它来恢复对我们应用程序上许多本地文件的访问。

https://github.com/TheMattRay/cordova-plugin-wkwebviewxhrfix

于 2017-04-17T14:53:21.083 回答