Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Add timeout for WKWebview

Tags:

ios

wkwebview

How to write a timeout handler for WKWebView, when default delegates are not getting called for didFailNavigation.

WKWebView delegate are set & DidFinishNavigation or didFailProvisionalNavigation is getting called.

like image 437
Anand Avatar asked Nov 05 '15 07:11

Anand


2 Answers

Use the error.code value of the error that didFailProvisionalNavigation creates and add your '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

        }
    }
like image 173
AT3D Avatar answered Oct 26 '22 07:10

AT3D


Use this delegate method

 webView:didFailProvisionalNavigation:withError:

Document

Invoked when an error occurs while starting to load data for the main frame.

And check the error code

NSURLErrorTimedOut = -1001

All the error code list

like image 28
Leo Avatar answered Oct 26 '22 09:10

Leo