Reputation: 6395
when iPhone connects to a wireless router, and that router is not connected to the internet? i need to display an error message saying "check you internet connection" i tried the Reachability sample code. but no luck, http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/ReadMe_txt.html
when i disable the WIFI in phone it's working fine, i tried the Checking For Internet Connectivity in Objective C sample code "isDataSourceAvailable" even itz not working,can any one help me to fix this issue, really appriciate.
Upvotes: 7
Views: 6509
Reputation: 4577
https://github.com/adib/asi-http-request/blob/master/External/Reachability/Reachability.h
This is a part of the ASI HTTPRequest project on git. It extends Apple's example and apparently is good enough for quite a few apps in the store (see ASI's wall of who's using: http://allseeing-i.com/ASIHTTPRequest/Who-is-using-it)
Anywho, I know an answer was already accepted, but just for further reference.
Upvotes: 0
Reputation: 24867
I recommend you do the same as Microsoft does, and to be really wicked, you can even use their servers, since they probably will make sure those are on line for the foreseeable future.
They look up a hostname and then access a very small file on a web server.
See the same question on serverfault (from a non programming perspective of course.)
Basically look up the IP address for a hostname (in that example "dns.msftncsi.com"), then access the URL, for example http://msftncsi.com/ncsi.txt. This can be done with simple socket programming if you like, real HTTP not necessary.
Open a socket to port 80, on the IP you found by looking up the hostname. Send a string to the socket like so:
"GET /msftncsi.com/ncsi.txt HTTP/1.1\nHost: msftncsi.com:80\n\n"
Then wait for something to return. If anything returns, even a single byte, it means you have Internet access.
Or at least access to this server, which in this example is Microsoft.
Upvotes: 1
Reputation: 104065
You could do something like this:
+ (BOOL) pingServer
{
NSURL *url = [NSURL URLWithString:@"http://some-url-that-has-to-work/"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSHTTPURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:NULL];
return (response != nil);
}
This is a synchronous request, so that it will block the current thread. You can do the whole network check in a background thread, so that blocking it won’t matter, or you can send the request asynchronously. Also you might want to set the HTTP method to HEAD, so that you won’t download the whole resource.
Upvotes: 10