ios - How to get webView didFail called - Stack Overflow

admin2025-04-09  1

I am working on a wrapper for WKWebView and want to make sure I handle possible errors.

How can I make WKNavigationDelegate's method

func webView(_: WKWebView, didFail _: WKNavigation!, withError error: Error)

called?

The first thing I tried was to call webView.load(request) with invalid url. As a result, func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) only was called -- not what I wanted.

The second thing I tried was to call webView.loadHTMLString(html, baseURL: nil) with malformed html. Nevertheless, webView displayed without throwing an error.

I am working on a wrapper for WKWebView and want to make sure I handle possible errors.

How can I make WKNavigationDelegate's method

func webView(_: WKWebView, didFail _: WKNavigation!, withError error: Error)

called?

The first thing I tried was to call webView.load(request) with invalid url. As a result, func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) only was called -- not what I wanted.

The second thing I tried was to call webView.loadHTMLString(html, baseURL: nil) with malformed html. Nevertheless, webView displayed without throwing an error.

Share Improve this question asked Mar 25 at 10:23 NickNick 3,46310 gold badges60 silver badges111 bronze badges 1
  • didFailProvisionalNavigation will be called if there is an error in the early navigation process Reference. I would try simulating a network issue or maybe a timeout. – tomerpacific Commented Mar 25 at 10:52
Add a comment  | 

1 Answer 1

Reset to default 1

You can load a valid URL and stop loading manually as soon as the navigation is committed.

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
 print("didCommit called - stopping navigation now.")
 webView.stopLoading()
}
    
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
 print("didFail navigation called")
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744203851a235925.html

最新回复(0)