如何在UIWebView中检测和处理HTTP错误代码? [英] How to detect and handle HTTP error codes in UIWebView?

查看:77
本文介绍了如何在UIWebView中检测和处理HTTP错误代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在收到HTTP错误404等时通知用户。我该怎么检测到?
我已经尝试实现

I want to inform user when HTTP error 404 etc is received. How can I detect that? I've already tried to implement

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

但是当我收到404错误时它没有被调用。

but it is not called when I receive 404 error.

推荐答案

您可以在此处捕获URLRequest:

You could capture the URLRequest here:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

并将请求交给代表并返回否。然后在收到的来自 NSURLConnection 的响应调用中取消连接,如果一切正常(检查响应),再次在webview中加载urlrequest。再次加载urlrequest时,请确保在上面的调用中返回YES。

and hand the request over to the delegate and return no. Then in the received response call from NSURLConnection cancel the connection and if everything is fine (check response) load the urlrequest once more in the webview. Make sure to return YES in the above call when loading the urlrequest again.

不是很优雅,但可能有效。

Not very elegant, but it might work.

这篇关于如何在UIWebView中检测和处理HTTP错误代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆