您好,我正在尝试使用 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)
最佳答案
要解决它,您需要从 WKPreferences
中打开 allowFileAccessFromFileURLs
swift
webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
objective-C
[webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
关于iOS - WKWebView 跨源请求仅支持 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996292/