Iphone connessione ad internet (raggiungibilità)
-
27-09-2019 - |
Domanda
ho visto alcun post sulla raggiungibilità ma la gente in realtà non dare la risposta esatta al problema. Nella mia applicazione uso il codice di raggiungibilità da Apple e nel mio AppDelegate usa questo:
-(BOOL)checkInternet {
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
BOOL internet;
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
internet = NO;
}else {
internet = YES;
}
return internet;
}
Quindi il problema è, anche se ho una connessione ad internet, questo codice mi dice che non ho uno. Qualcuno sa cosa fare per rendere questo lavoro?
Grazie,
Soluzione
Probabilmente si dovrebbe utilizzare +[Reachability reachabilityForInternetConnection]
piuttosto che raggiungibilità per un particolare nome (a meno che, naturalmente, che è quello che effettivamente bisogno).
Ci potrebbero essere tutti i tipi di ragioni che un determinato server potrebbe non essere raggiungibile, mentre si ha ancora una connessione internet attiva, dopo tutto.
Questo è quello che faccio:
BOOL hasInet;
Reachability *connectionMonitor = [Reachability reachabilityForInternetConnection];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(inetAvailabilityChanged:)
name: kReachabilityChangedNotification
object: connectionMonitor];
hasInet = [connectionMonitor currentReachabilityStatus] != NotReachable;
e quindi
-(void)inetAvailabilityChanged:(NSNotification *)notice {
Reachability *r = (Reachability *)[notice object];
hasInet = [r currentReachabilityStatus] != NotReachable;
}
, che funziona bene per me.
Altri suggerimenti
Utilizzare questo codice per verificare se il dispositivo è collegato a internet o no
utilizzare questo codice in viewDidLoad:
Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
[hostReachable startNotifier];
e aggiungere questa funzione per il vostro codice:
-(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);
}