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:
nevan
2010-07-28 15:53:14
In that example he uses the reachability class.
Chris
2010-07-28 17:21:56
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
2010-07-28 23:09:10
The problems were in the older versions of Apple's Reachability class - the latest version (v. 2.n.n) has been considerably rewritten.
NickFitz
2010-09-28 20:06:25
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
2010-09-26 06:42:55