swift将网页保存在wkwebview中

发布于 2025-01-30 07:47:40 字数 612 浏览 2 评论 0原文

我希望能够将任何网站保存在WKWebView中,并随时通过另一个按钮恢复它。

我尝试下载网站的HTML:

public func getHtml(completion: @escaping (String?, URL?) -> Void) {
        webView.evaluateJavaScript("document.documentElement.outerHTML.toString()") { html, error in
            let html = html as? String
            completion(html, self.webView.url)
        }
}

并使用以下方式恢复HTML webView.loadhtmlstring(html,baseurl:url)

但加载HTML会导致UI的格式失去格式:

https://i.sstatic.net/y3s7w.png 恢复URL的格式?

I want to be able to save any website in a WKWebView, and restore it at anytime through another button.

I tried downloading the HTML of the website:

public func getHtml(completion: @escaping (String?, URL?) -> Void) {
        webView.evaluateJavaScript("document.documentElement.outerHTML.toString()") { html, error in
            let html = html as? String
            completion(html, self.webView.url)
        }
}

and restoring the html using:
webView.loadHTMLString(html, baseURL: url)

But loading the html results in losing the formatting for the UI:
https://i.sstatic.net/Y3S7W.png

Is there a way to save / restore the formatting of the url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文