I am using AFNetworking 2.0 to track reachability.
In the viewDidLoad of my main VC, I have the following:
// Start monitoring the internet connection [[AFNetworkReachabilityManager sharedManager] startMonitoring]; [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); // Check the reachability status and show an alert if the internet connection is not available switch (status) { case -1: // AFNetworkReachabilityStatusUnknown = -1, NSLog(@"The reachability status is Unknown"); [self reachabilityNotReachableAlert]; case 0: // AFNetworkReachabilityStatusNotReachable = 0 NSLog(@"The reachability status is not reachable"); [self reachabilityNotReachableAlert]; case 1: // AFNetworkReachabilityStatusReachableViaWWAN = 1 NSLog(@"The reachability status is reachable via WWAN"); case 2: // AFNetworkReachabilityStatusReachableViaWiFi = 2 NSLog(@"The reachability status is reachable via WiFi"); break; default: break; } }];
In addition to this main VC, I load different controllers / paths / navigation controllers and fire them as soon as they are used.
Question What I'm trying to do is monitor the connection, but only when the main VC is displayed. For example, if I load the navigation controller over the main VC and the connection is lost, I still get a reachabilityNotReachableAlert call.
How can I track only when the main VC is displayed on the screen without having to start stopMonitoring and startMonitoring all the time?
I think I can put stopMonitoring in the prepareForSegue method and then startMonitoring in viewDidAppear , is there an easier way to do this?
source share