Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reachability Guide for iOS

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

like image 952
esqew Avatar asked Sep 24 '10 21:09

esqew


People also ask

What is iOS reachability?

Reachability is the name for the iPhone feature that brings the content halfway down your device's screen, which makes for more comfortable one-handed navigation. In older iPhones that featured a home button, Reachability could be activated by double-tapping on the Home Button.

What is reachability feature used for?

And what about "reachability"? Reachability was introduced three years ago with the iPhone 6 and 6 Plus, Apple's first larger-screen devices. Up until now, the feature has let you navigate the larger display with one hand by tapping on the home button twice.


2 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

like image 105
enbr Avatar answered Sep 20 '22 21:09

enbr


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.

like image 32
yabtzey Avatar answered Sep 24 '22 21:09

yabtzey