Using ASIHTTPRequest's Reachability

时光怂恿深爱的人放手 提交于 2019-12-11 20:08:45

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9185854/using-asihttprequests-reachability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!