25

When i return any http error from my page (currently 401, but i tried also with 404 and so on)

http://min60.com/__personal/e401.php

the delegate callbacks of the WKWebView don't return an error

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

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {

How to catch such errors?

2 Answers 2

47

The key was to wait for the response and then inspect the object, no error is called on http code

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {

    if ([navigationResponse.response isKindOfClass:[NSHTTPURLResponse class]]) {

        NSHTTPURLResponse * response = (NSHTTPURLResponse *)navigationResponse.response;
        if (response.statusCode == 401) {

            // here we go

        }

    }
    decisionHandler(WKNavigationResponsePolicyAllow);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Swift version:

public func webView(
    _ webView: WKWebView,
    decidePolicyFor navigationResponse: WKNavigationResponse,
    decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void
) {
    if
        let httpResponse = navigationResponse.response as? HTTPURLResponse,
        !(200..<400).contains(httpResponse.statusCode)
    {
        // Do something
    }

    decisionHandler(.allow)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.