Reachability Guide for iOS

Has anyone found half a decent guide on implementing Reachability on iOS?

+43
ios iphone reachability
Sep 24 '10 at 21:01
source share
2 answers

I implemented Reachability like this. Download https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html and add the Reachability.h and .m project to the project. Add the SystemConfiguration infrastructure 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; } 

If you want to check availability ...

 if ([self reachable]) { NSLog(@"Reachable"); } else { NSLog(@"Not Reachable"); } 

Here is an example I made. http://dl.dropbox.com/u/3656129/ReachabilityExample.zip

+95
Oct 17 2018-10-17
source share

I think the best way to check the availability of the host address is to check the results of the 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 error variable output, if it can access the URL request, the error is Nil.

Reachability gives a positive result, even if your URL packets can be routed from your device and never reach the host server.

+2
Aug 13 '13 at 16:32
source share



All Articles