2017-10-29 193 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") 
相關問題