views:

157

answers:

2

We are trying to display an alert view when there is no connection and we are using the reachability 2.2 classes from apple. The problem we are running into is that at the start of the program we are always getting an alert view that there is no internet connection but we are connected to the internet. Is there a correct way to check for the internet connection with these classes?

A: 

I remember reading that the Reachability code in the Apple code samples is not that great for doing network checks. The recommended approach was to check whether the device can see your website (or web page), and if not give an error.

I searched for where I read this, but couldn't find the original. Here's a different link which uses that approach:

http://www.iphonedevx.com/?p=657

nevan
In that example he uses the reachability class.
Chris
About half way down he says "I noticed that when first initialized the remoteHostStatus is always NotReachable" and describes refactoring the same code. Maybe the same as your problem?
nevan
The problems were in the older versions of Apple's Reachability class - the latest version (v. 2.n.n) has been considerably rewritten.
NickFitz
A: 

reachability need take some time to do its task. so be patient. using notification to get results.

This is what I do:

BOOL hasInet;
Reachability *connectionMonitor = [Reachability reachabilityForInternetConnection];
[[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(inetAvailabilityChanged:)
    name:  kReachabilityChangedNotification
    object: connectionMonitor];

hasInet = [connectionMonitor currentReachabilityStatus] != NotReachable;

and then

-(void)inetAvailabilityChanged:(NSNotification *)notice {
    Reachability *r = (Reachability *)[notice object];
    hasInet = [r currentReachabilityStatus] != NotReachable;
}

which works nicely for me.

xhan