1、拦截HTTPStatusCode
使用wkwebview加载网页时,诸如404这样的状态码并不会调起WKNavigationDelegate的失败的代理方法,那么如何做呢?
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
if (((NSHTTPURLResponse *)navigationResponse.response).statusCode == 200) {
decisionHandler (WKNavigationResponsePolicyAllow);
}else {
decisionHandler(WKNavigationResponsePolicyCancel);
}
}
上面的代码可以使得服务器返回200以外的状态码时,都调用请求失败的方法,从而可以做一些处理。