How to add a timeout for WKWebview

How to write a timeout handler for WKWebViewwhen delegates by default do not receive a call for didFailNavigation.

WKWebViewdelegated, and calls DidFinishNavigationor didFailProvisionalNavigation.

+4
source share
3 answers

Use the error.code value of the error generated by didFailProvisionalNavigation , and add the 'handler' code there:

func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {

        if error.code == -1001 { // TIMED OUT:

            // CODE to handle TIMEOUT

        } else if error.code == -1003 { // SERVER CANNOT BE FOUND

            // CODE to handle SERVER not found

        } else if error.code == -1100 { // URL NOT FOUND ON SERVER

            // CODE to handle URL not found

        }
    }
+9
source

Use this delegate method

 webView:didFailProvisionalNavigation:withError:

Document

Called when an error occurs while loading data for the main frame.

And check the error code

NSURLErrorTimedOut = -1001

All errors code list

+1
source

, , loadHTML, loadRequest

+1
source

Source: https://habr.com/ru/post/1614557/


All Articles