嗨,我正在尝试使用WKWebView加载主要Bundle中的HTML,这在UIWebView上完美运行,但现在出现错误,HTML文件中的文件不会加载,在控制台中发送错误:“XMLHttpRequest无法加载”
这是一个内嵌内容的示例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)
vistaweb.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
,该代码将允许通过文件URL访问文件。 - trungduc