iOS – WKWebView仅支持HTTP的跨源请求

嗨我正在尝试使用WKWebView加载一个主要Bundle中的html,这与UIWebView完美配合,但现在我有一个错误,html中的文件没有加载在控制台中发送错误:“XMLHttpRequest无法加载”

Consola形式模拟器

模拟器

这是嵌入内容的http://proyectoshm.com/esferas/dosaguas/dosaguascitta.html示例。

我的代码:

let folder = "maquetas/esferas/\(esfera)" let resourcePath = Bundle.main.resourcePath let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent(folder, isDirectory: true) guard let path = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: folder) else { print("no se encontró path") return } print(subdir.path) let url = NSURL.fileURL(withPath: path) print(url.path) vistaweb = WKWebView(frame: self.view.frame) vistaweb.loadFileURL(url, allowingReadAccessTo: subdir) self.view.addSubview(vistaweb) 

要解决此问题,您需要从WKPreferences

迅速

 webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs") 

Objective-C的

 [webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];