检查 NSURLConnection 是否失败
我正在使用 NSURLConnection 来从 Web 服务器获取一些 xml 文件。当服务器宕机时,我想通知用户。我找不到任何代表来检查请求是否成功。
据我了解 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
仅用于在实际下载期间接收错误。
有什么想法吗?
谢谢
I am using NSURLConnection in order to get some xml files from a web server. When the server is down, I want to inform the user. I cannot find any delegate for checking if the request did succeed or not.
As I understand it - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
is only for receiving errors during the actual download.
Any thoughts?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,如果您的服务器关闭,那么
方法将返回可能的原因&你可以提醒用户:)
yeah if your server is down then
method will return the possible reason & u can alert to user :)
如果无法建立连接以及下载中断,将调用 didFailWithError 。
有一个很好的类,称为可达性,您可以在尝试建立连接之前使用它来检查是否可以建立连接(以及如何通过 wifi、3g 等建立连接) - 您可以找到它 此处
didFailWithError will be called if the connection cannot be made as well as if it's interrupted in download.
There's a good class called reachability that you can use to check whether a connection could be made before attempting to establish one (also how it can be made i.e via wifi, 3g etc) - you can find it here
在这种情况下,您的
NSURLConnection
将无法正确初始化,并且您会得到初始化程序返回的nil
值。当然,您可以检查设备的可达性,但这不应该阻止您检查
nil
。Your
NSURLConnection
won't be initialized correctly in this case and you getnil
value returned by the initializer.Of course you can check device reachibility, but it shouldn't prevent you to check for
nil
.