Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView catch HTTP error codes with Swift 4

this question is identical to the following: WKWebView catch HTTP error codes; unfortunately the methods in Obj-C are not applicable to Swift 4, thus the cited WKNavigationResponse.response is no longer of type NSHTTPURLResponse so it doesn't have the http status code.

But the issue is still the same: I need to get the http status code of the answer to detect if the expected page is loaded or not.

Please note that webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) delegate is not called in case of 404 but only in case of network issue (i.e. server offline); the func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) is called instead.

Thanks a lot for your answers.

like image 831
Francesco Piraneo G. Avatar asked Mar 16 '18 13:03

Francesco Piraneo G.


People also ask

Can wkwebview catch HTTP error codes?

Bookmark this question. Show activity on this post. this question is identical to the following: WKWebView catch HTTP error codes; unfortunately the methods in Obj-C are not applicable to Swift 4, thus the cited WKNavigationResponse.response is no longer of type NSHTTPURLResponse so it doesn't have the http status code.

How to get the status code from wknavigationresponse?

Using a WKNavigationDelegate on the WKWebView you can get the status code from the response each time one is received. func webView (_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { if let response = navigationResponse.response as?

How to debug wkwebview error messages in Safari?

Instead the error reported is simply "Script error." The Safari Web Inspector is the best choice to debug WKWebView error messages for webpages that you do not control. Follow these steps to open the Safari Web Inspector during app development:

How do I enable JavaScript on a wkwebview?

You can ensure Javascript is enabled using: One method to capture Javascript errors from a WKWebView uses WKScriptMessageHandler and WKUserContentController. First, create a WKScriptMessageHandler by calling the add (_:, name:) method on the WKWebView.userControllerController:


2 Answers

Using a WKNavigationDelegate on the WKWebView you can get the status code from the response each time one is received.

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse,
             decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

    if let response = navigationResponse.response as? HTTPURLResponse {
        if response.statusCode == 401 {
            // ...
        }
    }
    decisionHandler(.allow)
}
like image 74
kumar reddy Avatar answered Sep 30 '22 01:09

kumar reddy


HTTPURLResponse is a subclass of URLResponse. The Swift way of “conditional downcasting” is the conditional cast as?, this can be combined with conditional binding if let:

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse,
             decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

    if let response = navigationResponse.response as? HTTPURLResponse {
        if response.statusCode == 401 {
            // ...
        }
    }
    decisionHandler(.allow)
}
like image 24
Martin R Avatar answered Sep 30 '22 02:09

Martin R