使用ASIHTTPRequest的可达性 [英] Using ASIHTTPRequest's Reachability

查看:83
本文介绍了使用ASIHTTPRequest的可达性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在我的iOS项目中使用可达性来测试是否存在互联网连接.

我的应用程序包含一个带有3个不同选项卡的UITabBarController.每个选项卡的根都是不同的UIViewController.在每个控制器中,我都会根据用户的操作发出多个请求.

我的问题是如何创建全局功能(或宏)以在使用Internet连接的每个功能之前使用,而不在每个控制器中都重写此功能.

是否可能具有执行此功能的功能?我看到有一种方法可以使应用程序根据网络状态发送通知.但是看这个例子:

  1. 用户点击UIButton
  2. 网络可用,请求开始.
  3. 如果网络中断,如何停止请求并显示例如UIAlertView?

我搜索的很好,但是谢谢,我找不到我的问题的答案.

解决方案

在提出网络请求之前,不应使用可访问性来检测网络是否可用,因为发出网络请求的行为会在需要时启动网络.可达性仅应用于检测网络不可用后何时可用.

只需尝试发出网络请求-如果网络不可用,您将收到一条错误消息.通常,响应会立即返回,但是如果网络不完整,则可能需要一段时间,因此您永远不应在主线程上进行同步网络调用.如果可能的话,请使用NSURLConnection,并且在发生某些情况时会得到回调.

I'm begin to use Reachability in my iOS project to test if there's internet connection or not.

My app consists in a UITabBarController with 3 different tabs. Each tab's root is a different UIViewController. In each controller I make several requests relying on user's actions.

My question is how to create a global function (or a macro) to use before each function that uses internet connection without rewriting this function in every controller.

Is possible to have a function that does this? I saw that there's a way to make the app sends notification relying on network status. But look at this example:

  1. The user tap a UIButton
  2. Network is available and the request starts.
  3. If network goes down, how to stop the request and display for example a UIAlertView?

I've searched well but I can't find answers to my questions, thanks.

解决方案

Reachability should not be used to detect if the network is available before making a network request because the act of making a network request can bring up the network if needed. Reachability should only be used to detect when the network becomes available after being unavailable.

Just try making the network request -- you'll get an error back if the network is unavailable. Usually the response will come back right away, but if the network is spotty it can take a while, so you shouldn't ever make synchronous network calls on the main thread. Use NSURLConnection if possible and you'll get callbacks when something happens.

这篇关于使用ASIHTTPRequest的可达性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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