2011-10-18 10 views
5

Estoy usando este código para comprobar si hay una conexión a Internet, pero estoy un accidente diciendo +[Reachability reachabilityForInternetConnection]: unrecognized selector sent to class 0xcbe0c8App estrellarse cuando se utilizan las clases Accesibilidad Los para comprobar si hay conexión a Internet

He importado Reachability .h/.m y el marco systemConfig. Crash está en línea 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; 
     } 
    } 
} 
+0

No, he hecho '@class Reachability' y también he importado – Jon

+0

Sí, he importado los archivos y he hecho' #import "Reachability.h" 'etc. – Jon

+0

Lo hice, es fallando en 'Reachability * newReach = [Reachability reachabilityForInternetConnection];' – Jon

Respuesta

1

Asegúrese de que su Reachability está en versión: 2.2, algunas cosas cambiaron recientemente y que pueden causar thiscrash si no usando 2.2.

Éstos son enlaces a version2.2 de Reachability.h y Reachability.m

Además, si ayuda, aquí está mi código de trabajo para esta misma tarea :.

En mi appDidFinishLaunching (hostReachable y internetReachable son Ivars de mi aplicación delegado):

//.... 
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]; 
} 

Entonces, la devolución de llamada:

- (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]; 
    } 
} 
-1

se debe girar a ARC fuera. Ir a construir fases, seleccione esta llama y haga doble clic en el borde derecho y el tipo

-fno -objc -arc 

Creo que se puede dejar a retener código también.

Cuestiones relacionadas