【发布时间】:2017-10-29 02:25:59
【问题描述】:
您好,我正在尝试使用 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") -
就像一个魅力,非常感谢!我从来没有找到这个
-
不客气 :)
标签: ios swift uiwebview wkwebview