I use the normal Reachability tools to check if there is an internet connection available. It works in most cases, but if I unplug the WAN cable to the wifi-router, for some reason it still say it can find the host by wifi. If I change the web address to something that doesn't exists, it will say "The internet is down", like it should. Where is my problem?
Example:
I. I have the "internet cable" unplugged, but phone connected by wifi to router. I run with this line, the first time for this domain:
hos开发者_如何学PythontReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
Result = No host found (correct)
II. I disable wifi in the phone. Result = Host found with WWAN (correct)
III. I enable wifi in the phone. Result = Host found with WIFI. (not correct, since router is not online)
For internet checking I send an http request to one site(most probably google.com) if it does not return true, it means no internet. This case works even the connection is so slow. Here is the method.
- (BOOL) connectedToInternet
{
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
return ( URLString != NULL ) ? YES : NO;
}
You may want to check out this thread. The Reachability code is known to only test connectivity with your router. There should be a notification for connectivity now.
精彩评论