reachabilityswift example objective-c ios reachability

objective-c - example - reachability swift 5



La accesibilidad no funciona como se esperaba (6)

Otra respuesta completa:

-(BOOL) isReachable:(NSString *)url { //Retrieve the host name by given url address. NSString *hostName = [[NSURL URLWithString:url] host]; Reachability *r = [Reachability reachabilityWithHostName:hostName]; if(NotReachable == [r currentReachabilityStatus]) { NSLog(@"Not Reachable"); return NO; } NSLog(@"Reachable"); return YES; }

Alcance descargado de Apple, usando este método para verificar si hay una conexión activa:

-(BOOL)isReachable{ Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"]; if(NotReachable == [r currentReachabilityStatus]) { NSLog(@"Not Reachable"); return NO; } NSLog(@"Reachable"); return YES; }

¿Devuelve NO cada vez a pesar de estar conectado? No puedo entender por qué ...

¿Algunas ideas? Gracias.

En una nota al margen, ¿alguien puede recomendar una buena clase de accesibilidad simple y accesible?


Si está intentando ver si el dispositivo puede acceder a Internet en general, probablemente debería usar reachabilityForInternetConnection en lugar de reachabilityWithHostName :. Además, estas dos llamadas tardarán un poco más en iniciarse (aún estará en milisegundos, pero será más largo que el tiempo necesario para alcanzar la condición if en la siguiente línea). Aquí hay un ejemplo de una clase de singleton que utiliza la accesibilidad.

static NetworkManager* sharedInstance = nil; @interface NetworkManager() @property (nonatomic, strong) Reachability* reachability; @end @implementation NetworkManager @synthesize reachability; + (NetworkManager*)sharedInstance { @synchronized(self) { if (sharedInstance == nil) { sharedInstance = [[NetworkManager alloc] init]; } } return sharedInstance; } - (id)init { reachability = [WYReachability reachabilityForInternetConnection]; } - (BOOL)isNetworkReachable { return ([self.reachability currentReachabilityStatus] != NotReachable); } @end

Para comprobar la red accesible en otras clases puede utilizar.

[[NetworkManager sharedInstance] isNetworkReachable];


Tengo el mismo problema con la capacidad de alcance de Tony Million: el estado de la red siempre se estableció en NotReachable. Lo arreglo con la adición del marco de configuración del sistema

Espero eso ayude


Use este código para verificar si el dispositivo está conectado a Internet o no

Usa este código en viewDidLoad:

Reachability* internetReachable; = [Reachability reachabilityForInternetConnection]; [internetReachable startNotifier]; hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ; [hostReachable startNotifier];

y agrega esta función a tu código:

-(void) checkNetworkStatus:(NSNotification *)notice { recheabilityBool=FALSE; nonrecheabilityBool=FALSE; // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { nonrecheabilityBool=TRUE; internetCon=0; //NSLog(@"The internet is down."); break; } case ReachableViaWiFi: { NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; internetCon=404; [prefs setInteger:internetCon forKey:@"conKey"]; //NSLog(@"The internet is working via WIFI."); break; } case ReachableViaWWAN: { //NSLog(@"The internet is working via WWAN."); break; } } NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; switch (hostStatus) { case NotReachable: { internetCon=0; if( nonrecheabilityBool==FALSE) { //NSLog(@"A gateway to the host server is down."); } break; } case ReachableViaWiFi: { if(recheabilityBool==FALSE) { recheabilityBool=TRUE; NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; internetCon=404; [prefs setInteger:internetCon forKey:@"conKey"]; //NSLog(@"The internet is working via WIFI."); break; } //NSLog(@"A gateway to the host server is working via WIFI."); break; } case ReachableViaWWAN: { //NSLog(@"A gateway to the host server is working via WWAN."); break; } } } - (BOOL)connected { Reachability *reachability = [Reachability reachabilityForInternetConnection]; NetworkStatus networkStatus = [reachability currentReachabilityStatus]; return !(networkStatus == NotReachable); }


Yo uso KSReachability . Funciona con NSNotification, blocks, KVO y con o sin ARC.

KSReachability *reachability = [KSReachability reachabilityToHost:@"www.google.com"]; reachability.onReachabilityChanged = ^(KSReachability* reachability) { NSLog(@"Update: network is %@", reachability.reachable ? @"up." : @"down."); };


EDITADO: debe eliminar el protocolo, http de su llamada reachabilityWithHostName , así que actualícelo a

Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"]; NetworkStatus netStatus = [reachability currentReachabilityStatus];