Reachability Guide for iOS
Asked Answered
B

3

43

Has anyone found a halfway decent guide to implementing Reachability on iOS?

Beverlybevers answered 24/9, 2010 at 21:1 Comment(0)
B
95

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

Borders answered 17/10, 2010 at 16:56 Comment(8)
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?Eiderdown
No. Not as far as I know. Just make sure the host you're checking is up and not blocked.Borders
When should you check for reachability? in applicationDidFinishLaunching?Christian
@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.Fourdrinier
Thanks for that code snippet. Is there a way to check for regular GSM connectivity?Authoritarian
@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.Sherrell
Thanks for this. It's amazing how hard it is to find a simple way of doing things in the iOS world, sometimes.Hagi
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.Quicklime
P
2

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.

Purlin answered 13/8, 2013 at 16:32 Comment(1)
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.Cautious
G
0

This question seems to have only obsolete answers. Since iOS 12 we have NWPathMonitor so you should at least look into that as well.

Gader answered 1/8, 2021 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.