如何从UIWebView获取最后一个HTTP状态代码? [英] How do I get the last HTTP Status Code from a UIWebView?

查看:123
本文介绍了如何从UIWebView获取最后一个HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测何时向UIWebView发送的页面加载请求在5xx或4xx范围内返回了状态代码。

I would like to detect when a page load request give to a UIWebView has returned a status code in the 5xx or 4xx range.

我为网络视图设置了委托,并提供了一个-webView:didFailLoadWithError:error方法,但是虽然它被调用超时,它不被调用HTTP状态代码错误。

I've setup the delegate for the web view and have provided a -webView:didFailLoadWithError:error method but although it gets called fine for timeouts, it is not called for HTTP Status Code errors.

有任何建议吗?

推荐答案

..我不是iPhone开发者,但是....

Hmmm... I'm not an iPhone developer, but....

您能尝试使用要加载的URL创建NSURLRequest吗?

Could you try creating an NSURLRequest with the URL you want to load? Then you could make the connection using NSURLConnection.

NSURLConnection有一个委托方法

NSURLConnection has a delegate method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

这将给出来自服务器的响应。请注意,如果您通过HTTP进行连接,响应实际上是NSHTTPURLResponse类。 NSHTTPURLResponse可用于使用以下实例方法获取状态

which will give the the response from the server. Please note that if you are making the connection over HTTP, the response will actually be of class NSHTTPURLResponse. The NSHTTPURLResponse can be used to get the status using the following instance method

- (NSInteger)statusCode

NSURLConnection有另一个委托方法

NSURLConnection has another delegate method

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

用于从URL连接获取数据。你可以手动加载数据到你的UIWebView使用:

that can be used to get the data from the URL Connection. You could then manually load the data into your UIWebView using:

- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL

一吨的工作,但它可以做到。希望别人会拿出更容易的方式,虽然我看不到。

That seems like a ton of work, but it could be done. Hopefully someone else will come up with the easier way, though I don't see it.

这篇关于如何从UIWebView获取最后一个HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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