views:

66

answers:

2

Hi 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 http://stackoverflow.com/questions/477852/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.

+2  A: 

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.

zoul
Thx for the reply this help me to fix this did few modification to method you gave.
Sam
A: 

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.

Amigable Clark Kant