使用 ASIHTTPRequest 的可达性
我开始在我的 iOS 项目中使用 Reachability 来测试是否有互联网连接。
我的应用程序包含一个带有 3 个不同选项卡的 UITabBarController。每个选项卡的根都是不同的UIViewController
。在每个控制器中,我根据用户的操作发出多个请求。
我的问题是如何创建一个全局函数(或宏)以在使用互联网连接的每个函数之前使用,而无需在每个控制器中重写此函数。
有可能有一个函数可以做到这一点吗?我看到有一种方法可以让应用程序根据网络状态发送通知。但请看这个示例:
- 用户点击
UIButton
- 网络可用并且请求开始。
- 如果网络出现故障,如何停止请求并显示例如
UIAlertView
?
我已经搜索得很好,但找不到我的问题的答案,谢谢。
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:
- The user tap a
UIButton
- Network is available and the request starts.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在发出网络请求之前,不应使用可达性来检测网络是否可用,因为发出网络请求的行为可以在需要时启动网络。可达性仅应用于检测网络在不可用后何时变得可用。
只需尝试发出网络请求 - 如果网络不可用,您将收到错误消息。通常响应会立即返回,但如果网络不稳定,则可能需要一段时间,因此您不应该在主线程上进行同步网络调用。如果可能的话使用 NSURLConnection ,当有事情发生时你会得到回调。
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.