5

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;
        }
    }
}
Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
Jon
  • 4,732
  • 6
  • 44
  • 67
  • No, I've done `@class Reachability` and i've imported as well – Jon Oct 18 '11 at 19:06
  • Yes, ive imported the files and I've done `#import "Reachability.h"` etc. – Jon Oct 18 '11 at 19:14
  • I did that, its crashing at `Reachability *newReach = [Reachability reachabilityForInternetConnection];` – Jon Oct 18 '11 at 20:02
  • I'm getting this error now after upgrading from Reachability 2.0 to 2.2: ` *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbf0c4'` – Jon Oct 18 '11 at 20:30
  • Two more questions that are shots in the dark. 1) Are your source files corrupted/changed from original apple files? 2) If you're using a workspace in xCode sometimes the files will be in the workspace but not in the project. When you collapse the projects discloser triangle it's files will collapse away. Is that case? – NJones Oct 18 '11 at 20:34
  • One more thought. Reachability should be listed under Compile Sources in the project pane with your target selected. – NJones Oct 18 '11 at 20:37

2 Answers2

1

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];
    }
}
chown
  • 51,908
  • 16
  • 134
  • 170
-1

You should turn ARC off. Go to build phases, select this calls and double click at the right edge and type

-fno -objc -arc

I think you can drop off retain code too.

yabtzey
  • 391
  • 1
  • 7
  • 17