Invalid status for accessibility in appDidBecomeActive

I am using the Reachability class that Apple provided and encountered one weird thing. The app checks the connection every time the app becomes active, and if it is active, refresh some data. When I enable airplane mode and immediately after that restart the app, so BecomeActive will be called, reachability returns wrong status (ViaWiFi reachable). And if you repeat this one more time, the correct status will return.

Also I noticed that if you go to airplane mode, wait a few seconds and then run the app again, then reachability will return the correct status.

Are there any explanations for this behavior?

0


source to share


1 answer


You need to be more stringent when you check connectivity. Add another condition while you receive an availability change notification.

Check the conditions below:



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

      

0


source







All Articles