Sto cercando di ottenere l'autentico stato di connettività Internet. Ho usato la Raggiungibilità di Apple, ma mi sta solo fornendo lo stato della connettività Wi-Fi, ad esempio non coprendo il caso quando il dispositivo è connesso via wifi (a un router o hotspot), ma il router o hotspot wifi non è connesso a Internet. Questo scenario è riproducibile estraendo il cavo di input Internet dal router wifi. Il notificante della raggiungibilità restituisce ReachableViaWiFi
sia per reachabilityForInternetConnection
sia per ReachabilityWithHostName
. Sono abbastanza in una correzione con questo problema. L'ho provato tramite lo NSURLConnection
, ma questo sta scaricando troppo la batteria e personalmente non mi piace questa soluzione per continuare a fare richieste URL, anche se in un thread in background.Come rilevare la perdita di raggiungibilità di Internet quando la sorgente wifi perde la connessione?
Ecco il codice che sto usando (per gentile concessione di un SO compagni, non ricordo il link esatto però)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
[hostReachable startNotifier];
poi nel metodo osservatore:
- (void) checkNetworkStatus:(NSNotification *)notice{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
self.isInternetReachable = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
self.isInternetReachable = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
self.isInternetReachable = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
self.isHostReachable = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
self.isHostReachable = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
self.isHostReachable = YES;
break;
}
}
}
Hi @Ahmed. Hai mai trovato una soluzione a questo? Ho lo stesso identico problema: i miei utenti hanno i propri dispositivi connessi a un hotspot Wi-Fi e quando l'hotspot perde la connettività, l'app non lo sa. –
2016 e ancora è irrisolto, ha avuto lo stesso problema. –