Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashing when using Reachability Classes to check for internet connection

I'm using this code to check for an internet connection but I'm getting a crash saying +[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbe0c8

I've imported Reachability .h/.m and the systemconfig framework. Crash is at line self.internetRechable = [[Reachability reachabilityForInternetConnection] retain];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    self.internetRechable = [[Reachability reachabilityForInternetConnection] retain];
    [self.internetRechable startNotifier];

    // check if a pathway to a random host exists
    self.hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
    [self.hostReachable startNotifier];

- (void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes

    NetworkStatus internetStatus = [self.internetRechable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
//            self.internetActive = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
//            self.internetActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
//            self.internetActive = YES;
            break;
        }
    }

    NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
//            self.hostActive = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
//            self.hostActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
//            self.hostActive = YES;
            break;
        }
    }
}
like image 757
Jon Avatar asked Nov 25 '22 08:11

Jon


1 Answers

Make sure your Reachability is at version: 2.2, a few things changed recently that may cause thiscrash if your not using 2.2.

Here are links to version2.2 of Reachability.h and Reachability.m

Also, if it helps, heres my working code for this same task:.

In my appDidFinishLaunching (hostReachable and internetReachable are ivars of my app delegate):

//....
if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == NotReachable) {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];
    hostReachable = [[Reachability reachabilityWithHostName:@"google.com"] retain];
    [hostReachable startNotifier];
}

Then, the callback:

- (void)checkNetworkStatus:(NSNotification *)notice {
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus) {
        case NotReachable:
            self.internetActive = NO;
            break;
        case ReachableViaWiFi:
            self.internetActive = YES;
            break;
        case ReachableViaWWAN:
            self.internetActive = YES;
            break;
    }
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus) {
        case NotReachable:
            self.hostActive = NO;
            break;
        case ReachableViaWiFi:
            self.hostActive = YES;
            break;
        case ReachableViaWWAN:
            self.hostActive = YES;
            break;
    }
    if (internetActive && hostActive) {
        [self refreshAllData];
    }
}
like image 197
chown Avatar answered Dec 09 '22 22:12

chown