Search code examples
iphoneobjective-cioscocoa-touchreachability

Can't get Apple Reachability class to define networking right


I have a classic Apple reachability class which is not adapted to ARC. It defines the network pretty well but it has some mistake that I can't figure out. I've left the whole class unchanged and in the method of another class, I have implemented the following methods to define the connectivity. Here's my BOOL value that changes whenever the internet is available:

- (BOOL) checkForInternetConnection {

    [self checkNetworkStatus:nil];
    if (isConnection || is3G) {
        return YES;
    } else {
        return NO;
    }
}

And here's the standard Reachability method:

-(void) checkNetworkStatus:(NSNotification *)notice
{

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            isConnection = NO;
            is3G = NO;
        }
        case ReachableViaWiFi:
        {
            isConnection = YES;
            is3G = NO;
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"Connected via 3G");
            is3G = YES;
            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            isConnection = NO;
            is3G = NO;
            NSLog(@"No Network");
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"Connected via WiFi");
            isConnection = YES;
            is3G = NO;
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"Connected via 3G");
            is3G = YES;
            break;
        }
    }
}

The problem I'm getting is well seen in the NSLog output I get:

2012-12-24 11:19:41.045 Custom Queue[1723:907] Connected via 3G
2012-12-24 11:19:41.046 Custom Queue[1723:907] No Network
2012-12-24 11:19:41.047 Custom Queue[1723:907] Connected via 3G
2012-12-24 11:19:41.048 Custom Queue[1723:907] No Network

After a few seconds it informs me that all is well:

2012-12-24 11:20:11.101 Custom Queue[1723:907] Connected via 3G
2012-12-24 11:20:11.112 Custom Queue[1723:907] Connected via 3G
2012-12-24 11:20:11.113 Custom Queue[1723:907] Connected via 3G

But due to the fact that it thinks that the host is unreachable and gives me the message that there's no connection, I have another function that fires when the Internet is unavailable.

How do I change it so it sees the Internet connection faster, without that occasional "no network" message?


Solution

  • I ended up using Reachability version from Tony Million. It's great and easy to use. Moreover, it's ARC and iOS 5 (and up) ready.

    You can get it here:

    https://github.com/tonymillion/Reachability

    Thanks, Tony!