[英]Can not load local html file in WKWebView Swift 4
I used this code before - everything worked 我以前使用过此代码-一切正常
let localfilePath = Bundle.main.url(forResource: "nameFile", withExtension: "html")
let myRequest = NSURLRequest(url: localfilePath!)
WebView.loadRequest(myRequest as URLRequest)
self.view.addSubview(WebView)
Help please - now I use WKWebView
. 请帮助-现在我使用
WKWebView
。
How to open a local html file in the WKWebView
? 如何在
WKWebView
打开本地html文件? Thank you 谢谢
import WebKit
@IBOutlet weak var webView: WKWebView!
let htmlPath = Bundle.main.path(forResource: "NameFolder/FileName", ofType: "html")
let url = URL(fileURLWithPath: htmlPath!)
let request = URLRequest(url: url)
webView.load(request)
Do not forget to put your folder with the files here 不要忘记将文件夹和文件放在这里
WKWebView has a method specific for loading local urls . WKWebView有特定于加载本地URL的方法 。 No need to use a URLRequest, just pass your local file url.
无需使用URLRequest,只需传递您的本地文件url。 Btw naming a url object localfilePath is misleading.
顺便说一句,URL对象localfilePath的命名具有误导性。 I suggest changing it to localfileURL.
我建议将其更改为localfileURL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.