Checking Internet connection with Reachability 2.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 g开发者_如何学运维etting 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?
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
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.
精彩评论