Reputation: 1039
Across my app I am using NSURLConnection to make any server related API Calls.
I am using the function NSURLConnection sendAsynchronousRequest queue completionHandler
to make async requests to the server. Sometimes these requests fail because there is no internet connection.
How would I use Apple's Reachability Class in conjunction with NSURLConnection?
Upvotes: 0
Views: 66
Reputation: 10417
The correct way to use reachability is as follows:
What you absolutely should not do is gate the initial request on reachability. The reachability APIs can get out of sync with reality, causing your app to fail to make requests that would otherwise have succeeded.
It is, however, acceptable to limit the retry rate of periodic requests based on overall reachability. However, if any of those requests succeeds, your app should then act as though reachability gave it a green light even if it didn't.
Upvotes: 0
Reputation: 176
You just need to do that:
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable) {
// connection ok: call NSURLConnection sendAsynchronousRequest queue completionHandler here
}
else {
// No connection
}
Be careful to check if the network is provided by wifi or not before download big data. You can check this through the internetStatus.
Upvotes: 1