问题
I'm using Reachability class, which Apple provided and faced one strange thing. App checks connection every time app did become active, and if it's active, update some data. When i turn on airplane mode and just after that relaunch the app, so didBecomeActive will be called, reachability returns wrong status(reachableViaWiFi). And if you repeat this one more time, correct status is returned.
Also i've noticed, that if you turn airplane mode, wait for a several seconds and then relaunch the app, reachability returns correct status.
Is there any explanation for such behaviour?
回答1:
You need to more strict when you check for connectivity. Add more condition while you receive reachability change notification.
Check below conditions:
- (void)reachabilityDidChange:(NSNotification *)notification {
// Reachbility for internet
Reachability *reachability = (Reachability *)[notification object];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
// Reachbility for host
Reachability *hostReachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus hostStatus = [hostReachability currentReachabilityStatus];
switch (hostStatus) {
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
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;
}
}
}
来源:https://stackoverflow.com/questions/26675427/wrong-status-for-reachability-in-appdidbecomeactive