WKWebView捕获HTTP错误代码

Pet*_*isu 18 cocoa-touch objective-c ios wkwebview

当我从我的页面返回任何http错误(目前是401,但我也试过404等等)

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

WKWebView的委托回调不会返回错误

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

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
Run Code Online (Sandbox Code Playgroud)

如何捕捉到这样的错误?

Pet*_*isu 37

关键是等待响应,然后检查对象,http代码没有调用错误

- (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);
}
Run Code Online (Sandbox Code Playgroud)