Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView does not finish/timeout when there is no internet access

There is a remote Javscript asset tag on a local HTML page I'm navigating to via WkWebView that never finishes downloading (in both the case that the iOS device is not connected to the internet, or the device's internet is too slow).

Unfortunately, the WKWebView never aborts trying to download this asset nor does it ever move on to my WKNavigationDelegate (i.e. neither -webView:didFailNavigation:withError: nor -webView:didFinishNavigation: neither are called), so users are stuck with a blank HTML page that never fully resolves and never errors out.

Thankfully in some scenarios I've debugged via the Safari remote iOS Javascript debugger, I've noticed we get the error Failed to load resource: The Internet connection appears to be offline logged to the console, but in others (like certain devices in Airplane Mode) the WKWebView never seems to timeout with this error or give up, so no WKNavigationDelegate callbacks get fired.

You can reproduce this scenario on your own by using the iOS 8.2 simulator with any WKWebView iOS application, navigate to any locally served HTML file, then add a script tag with any remote URL, then throttle your Internet connection to "100% Loss" using Apple's Network Link Conditioner.

What would be the best way to force a WkWebView to timeout/return via an error or success callback to its WKNavigationDelegate?

like image 231
depthfirstdesigner Avatar asked Mar 10 '15 18:03

depthfirstdesigner


1 Answers

Try:

WKNavigationDelegate

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error

You can check the error, usually you will have an error.code -1009:

Check this list: Error Codes

like image 91
Gabriel.Massana Avatar answered Nov 07 '22 16:11

Gabriel.Massana