ok, I found out the answer to this - Apple's reachability does not test actual connectivity to the host. See the answer by @Zhami in the SO link below:
How to write a simple Ping method in Cocoa/Objective-C
Essentially, when you first launch the app and do a reachability check, iOS seems to do a DNS lookup, and if there is no internet, the check fails. So the first time you check reachability , it actually returns a meaningful value. However, if you are conected at app launch, and lose internet connectivity after some time (while still connected to WiFi/3G/4G but no underlying internet connectivity), further reachability checks return reachable even though the internet or your specified host is not reachable anymore.
So if you want to really check for connectivity in real time, consider using the following:
-(BOOL) isConnected
{
NSString* url = [NSURL URLWithString:@"http://www.google.com/m"];
ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeOutSeconds:10];
//customize as per your needs - note this check is synchronous so you dont want to block the main thread for too long
[request setNumberOfTimesToRetryOnTimeout:0];
[request startSynchronous];
NSError *error = [request error];
if (error)
{
DLog(@"connectivity error");
return NO;
}
else
{
DLog(@"connectivity OK");
return YES;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…