WKWebView捕获HTTP错误代码 [英] WKWebView catch HTTP error codes
本文介绍了WKWebView捕获HTTP错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我从我的页面返回任何http错误(目前为401,但我尝试了404等)
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
代表WKWebView的回调不返回错误
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 {
如何捕获此类错误?
推荐答案
关键是等待响应,然后检查对象,没有调用http代码的错误
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);
}
这篇关于WKWebView捕获HTTP错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文