问题
I was download my website from online to store in xcode project with a sub-folder and want to run in WKWebView locally. I found the webview loaded webpage miss all CSS/JS/Images resources. I search around the internet and still not working. Is webpage inside relative url incorrect or my code to get the Main Bundle location problem? Anyone can help. Thanks.
Xcode Project Structure
HTML Page Contents Sample
I wrote this code to load WKWebView
let htmlFile = Bundle.main.path(forResource: "provisiontech", ofType: "htm")
let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
let baseURL = Bundle.main.resourceURL!.appendingPathComponent("pvtc")
webView.loadHTMLString(html!, baseURL: baseURL)
回答1:
The webview has no access to the resource folder. You should use loadFileURL(_:allowingReadAccessTo:) instead. Preloading the data is not neccessary, e.g.:
let url = Bundle.main.url(forResource: "provisiontech", ofType: "htm")
webView.loadFileURL(url, allowingReadAccessTo:
Bundle.main.resourceURL!.appendingPathComponent("pvtc"))
Edit: If the webview still cannot access the resources, you should consider whether putting the page and all resources into the same folder.
来源:https://stackoverflow.com/questions/45801178/swift-3-wkwebview-load-local-html-not-loading-resource-files