WKWebView捕获HTTP错误代码
当我从我的页面返回任何http错误(目前401,但我也尝试与404等)
http://min60.com/__personal/e401.php
WKWebView的委托callback不会返回错误
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
如何捕捉这样的错误?
关键是等待响应,然后检查对象,没有错误被称为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); }