Search code examples
iosreachability

Testing the internet connection is not working with Reachability


I am using the updated Reachability library to test whether the internet connexion is reachable. I try to Log a message in case the internet is not reachable, but the Log doesn't debug:

//Test the internet connection
Reachability* reach = [Reachability reachabilityForInternetConnection];
reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"Internet connexion unreachable");//Although Internet cnx is off, this message is not displayed
    return;
};

// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];

Am I misunderstanding the Reachability library? How to perform a given task when Internet is off? Thanx.

P.S: My iPad is only wifi, without 3G service.


Solution

  • Ok, so best way I got is following Apple sample code:

       //Test the internet connection
       Reachability* reach = [Reachability reachabilityForInternetConnection];
    
       NetworkStatus netStatus = [reach currentReachabilityStatus];
       //See if the reachable object status is "ReachableViaWifi"
    if (netStatus!=ReachableViaWiFi) {
        //If not
        NSLog(@"wifi unavailable");
        //Alert the user about the Internet cnx
        WBErrorNoticeView *notice = [WBErrorNoticeView errorNoticeInView:self.view title:@"Network Error" message:@"Check your internet connection."];
        notice.sticky = NO;
        [notice show];
        return;//Exit the method
    }