Has anyone found a halfway decent guide to implementing Reachability on iOS?
3 Answers
I have implemented Reachability like this. Download https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html and add Reachability.h and .m to your project. Add the SystemConfiguration framework to your project. #import "Reachability.h" where you want to use it. Use this code.
-(BOOL)reachable {
Reachability *r = [Reachability reachabilityWithHostName:@"enbr.co.cc"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable) {
return NO;
}
return YES;
}
When you want to check for reachability...
if ([self reachable]) {
NSLog(@"Reachable");
}
else {
NSLog(@"Not Reachable");
}
Here is the example project that I made. http://dl.dropbox.com/u/3656129/ReachabilityExample.zip
-
Hey, I'm using a version of this code, and its working nicely. All the other information i find on the internet seems to use notifications, and for WifiReach, InternetReach, and Host reach. This seems like a much more clean way to go. Is there a downside i'm not seeing? – averydev Nov 11 '10 at 04:30
-
No. Not as far as I know. Just make sure the host you're checking is up and not blocked. – enbr Nov 12 '10 at 05:34
-
When should you check for reachability? in applicationDidFinishLaunching? – Sheehan Alam Jun 26 '11 at 02:10
-
1@AVeryDev - the notifications thing is from Apple's own reachability example. It tracks internet connectivity, and notifies the app when the device switched connections. Use if your app is interested in changes to connectivity. Otherwise the above is obviously a lot simpler. – n13 Aug 17 '11 at 07:09
-
Thanks for that code snippet. Is there a way to check for regular GSM connectivity? – seeafish Aug 21 '11 at 10:03
-
@Sheehan Alam- you should check for reachability any time you need to do something where a connection is required. Namely, if something fails when your phone is in airplane mode (or your laptop's wifi is off and you're in Simulator) it shouldn't do so silently if the user expects a result. – Eric G Sep 13 '11 at 05:46
-
Thanks for this. It's amazing how hard it is to find a simple way of doing things in the iOS world, sometimes. – MusiGenesis Oct 19 '12 at 15:23
-
This code ends up calling SCNetworkReachabilityGetFlags synchronously which can block for up to 30 seconds while resolving a DNS name. If so, the watchdog will kill your app. Apple explicitly recommends against doing this in the Reachability's ReadMe.txt file. This code is dangerous if called on the main thread. That's why you see notifications used everywhere else. – Jeremy May 21 '13 at 21:20
I think the best way to check the availability of host address is by checking the results of NSURL Request.
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &error];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
Using this bit of Code, if your device cannot reach the provided URL, it provides some output to the error variable, if it can access the URL Request, error is Nil.
Reachability gives a positive output even if you URL packets can route out from your device and never reach the host server.

- 391
- 1
- 7
- 17
-
Typically you would just make a HEAD request if you only wanted to check that a resource is available. If you are only going to download it any way then sure, just try to download it. – hooleyhoop Aug 15 '13 at 11:03
This question seems to have only obsolete answers. Since iOS 12 we have NWPathMonitor so you should at least look into that as well.

- 1,427
- 1
- 12
- 30