V
V
Votetoda2018-08-07 09:30:23
iOS
Votetoda, 2018-08-07 09:30:23

How to reload webview only after page is loaded?

How to reload Webview only after the page is loaded ? That is, I have content in the webview, and when data changes, I need to refresh the webview itself when the data is ready for replacement, that is, do not show a white screen and gradually load the page.
webView = WKWebView(frame: webView.frame)
view.addSubview(webView)
webView.navigationDelegate = self
webView.load(myRequest)
The option to use 2 webviews and change them with each other, well, such a solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Beltyukov, 2018-08-09
@maestrro712

I think this can be done by canceling the load in the delegate method and doing it manually:

func webView(_ webView: WKWebView,
                 decidePolicyFor navigationAction: WKNavigationAction,
                 decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        URLSession.shared.dataTask(with: navigationAction.request) { [weak webView] (data, _, _) in
            decisionHandler(.cancel)
            guard let html = data.flatMap({ String(data: $0, encoding: .utf8) }) else {
                return
            }
            webView?.loadHTMLString(html, baseURL: navigationAction.request.url)
        }.resume()
    }

Naturally, in such a situation, it is necessary to carefully work out all sorts of boundary conditions, for example, redirects.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question