문제

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:

hostReachable = [[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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top