问题
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.
回答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