Hug*_*tis 5 ios swift wkwebview
我有一个完全在模拟器中工作的WKWebView,但为什么在设备中只运行一个白色空屏幕,这是我的代码:
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences.setValue(true, forKey:"allowFileAccessFromFileURLs")
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let path2Esferas = Path.localPath.stringByAppendingPathComponent(path: "\(Path.DIR_IMAGES)\(ImagenDescargaTipo.esfera.rawValue)/\(desarrollo.id)/virtualtour.html")
let url = URL(fileURLWithPath: path2Esferas)
//NSURL.fileURL(withPath: path2Esferas)
let myRequest = URLRequest(url: url)
webView.load(myRequest)
}
Run Code Online (Sandbox Code Playgroud)
在"path2Esferas"的HTML是在高速缓存中的文件夹,我以前加载,如果我把不同的URL一样let myURL = URL(string: "https://www.apple.com")的文档显示,它工作正常在模拟器和设备.例如:http://proyectoshm.com/esferas/real_de_mina/realdeminas.html
一年多后我找到了解决方案,我们需要使用loadFileURL来访问本地资源,这是我使用 WKWebView 的工作代码,加上我使用 loadHTMLString 而不是加载。顺便说一句,我的 WebView 中的vistaweb
感谢这个答案:Load local web files & resources in WKWebView
do {
let local = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
// Loading: Library/Caches/repositorioLocal/esferas/znvUifZhH8mIDr09cX8j/index.html
let resourceFolder = "repositorioLocal/esferas/znvUifZhH8mIDr09cX8j"
let fileToLoad = "index.html"
let urlToFolder = URL(fileURLWithPath: local).appendingPathComponent(resourceFolder)
let urlToFile = URL(fileURLWithPath: local).appendingPathComponent(resourceFolder).appendingPathComponent(fileToLoad)
let fileContent = try String(contentsOf: urlToFile)
let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
webConfiguration.allowsInlineMediaPlayback = true
vistaweb = WKWebView(frame: self.view.frame, configuration: webConfiguration)
self.vistaweb.loadFileURL(urlToFolder, allowingReadAccessTo: urlToFolder)
self.vistaweb.loadHTMLString(fileContent, baseURL: urlToFile)
self.view.addSubview(vistaweb)
} catch let error {
print("Error: \(error.localizedDescription)")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1083 次 |
| 最近记录: |