kReachabilityChangedNotification is called multiple times

江枫思渺然 提交于 2019-12-05 04:05:51

Add the reachability notification in the appdelegate didFinishLaunchingWithOptions only and set the value of reachability in Bool variable isInternetAvailable in appDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(checkNetworkStatus:)
                                                 name:kReachabilityChangedNotification object:nil];

}


- (void)checkNetworkStatus:(NSNotification *)notice {
    // called after network status changes

    NetworkStatus internetStatus = [self.internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            //#######NSLog(@"The internet is down.");
            self.isInternetAvailable = FALSE;
            break;
        }
        case ReachableViaWiFi:
        {
            //#######NSLog(@"The internet is working via WIFI");
            self.isInternetAvailable = TRUE;
            break;
        }
        case ReachableViaWWAN:
        {
            //#######NSLog(@"The internet is working via WWAN!");
            self.isInternetAvailable = TRUE;
            break;
        }

        default:
        {
            //#######NSLog(@"The internet is working via mobile SIM!");
            self.isInternetAvailable = FALSE;
            break;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!