使用 ASIHTTPRequest 的可达性

发布于 2025-01-03 01:09:52 字数 463 浏览 3 评论 0原文

我开始在我的 iOS 项目中使用 Reachability 来测试是否有互联网连接。

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

我的问题是如何创建一个全局函数(或宏)以在使用互联网连接的每个函数之前使用,而无需在每个控制器中重写此函数。

有可能有一个函数可以做到这一点吗?我看到有一种方法可以让应用程序根据网络状态发送通知。但请看这个示例:

  1. 用户点击 UIButton
  2. 网络可用并且请求开始。
  3. 如果网络出现故障,如何停止请求并显示例如 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:

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2025-01-10 01:09:52

在发出网络请求之前,不应使用可达性来检测网络是否可用,因为发出网络请求的行为可以在需要时启动网络。可达性仅应用于检测网络在不可用后何时变得可用。

只需尝试发出网络请求 - 如果网络不可用,您将收到错误消息。通常响应会立即返回,但如果网络不稳定,则可能需要一段时间,因此您不应该在主线程上进行同步网络调用。如果可能的话使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文