Reachability sometimes fails, even when we do have an internet connection

Posted by stoutyhk on Stack Overflow See other posts from Stack Overflow or by stoutyhk
Published on 2009-08-15T05:24:27Z Indexed on 2010/06/12 0:22 UTC
Read the original article Hit count: 555

Filed under:
|

Hi

I've searched but can't see a similar question.

I've added a method to check for an internet connection per the Reachability example. It works most of the time, but when installed on the iPhone, it quite often fails even when I do have internet connectivity (only when on 3G/EDGE - WiFi is OK).

Basically the code below returns NO.

If I switch to another app, say Mail or Safari, and connect, then switch back to the app, then the code says the internet is reachable. Kinda seems like it needs a 'nudge'.

Anyone seen this before? Any ideas?

Many thanks James

+ (BOOL) doWeHaveInternetConnection{

BOOL success;
// google should always be up right?!
const char *host_name = [@"google.com" cStringUsingEncoding:NSASCIIStringEncoding];

SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
																			host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
BOOL isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);

if (isAvailable) {
	NSLog(@"Google is reachable: %d", flags);
}else{
	NSLog(@"Google is unreachable");
}

return isAvailable;

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c