Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS network reachability - doesn't seem to be working

I'm following How to check for an active Internet connection on iOS or OSX? and http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html to test for connectivity, but am running into some pretty basic issues.

For example, I'm running a .NET API off of another machine and then trying to connect to it via my mac mini.

1) When IIS is running, everything is working like it should, I was excited that I got this working!

2) I can shut off IIS completely (obviously, host is unreachable) but my reachabilityWithHostName is still reporting that the host is reachable.

Any ideas?

like image 249
Buchannon Avatar asked May 03 '11 19:05

Buchannon


2 Answers

See the SCNetworkReachability Reference.

The SCNetworkReachability programming interface allows an application to determine the status of a system's current network configuration and the reachability of a target host. A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host.

Turning IIS on and off is just preventing your server from receiving web request such as ftp/http and does not stop the device from successfully sending a data packet out.

like image 180
Joe Avatar answered Oct 05 '22 22:10

Joe


A while ago I used this answer to solve my problem. I found a better solution and I am posting it here for others who may find it helpful.

There is a nice sample code provided by Apple.

Download the sample code here

Include the Reachability.h and Reachability.m files in your project. Take a look at ReachabilityAppDelegate.m to see an example on how to determine host reachability, reachability by WiFi, by WWAN etc. For a very simply check of network reachability, you can do something like this

Reachability *networkReachability = [Reachability reachabilityForInternetConnection];   
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];    
if (networkStatus == NotReachable) {        
    NSLog(@"There IS NO internet connection");        
} else {        

     NSLog(@"There IS internet connection");        


    }        
}
like image 39
Semere Taézaz Sium Avatar answered Oct 05 '22 23:10

Semere Taézaz Sium