2017-10-29 198 views
0

嗨,我想加载一个HTML的主包与WKWebView这与UIWebView完美的工作,但现在我有一个错误,HTML中的文件不会加载在控制台发送一个错误: “的XMLHttpRequest无法加载”iOS WKWebView交叉来源请求只支持HTTP

Consola form simulator

Simulator

这是示例嵌入的内容的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) 
+1

试试这个'vistaweb.configuration.preferences.setValue(真,forKey: “allowFileAccessFromFileURLs”)' – trungduc

+0

就像一个魅力,非常感谢!我从来没有发现这个 –

+0

不客气:) – trungduc

回答

2

此代码将帮助你在这种情况下:)

vistaweb.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs") 
相关问题