Monitoring Reachability with AFNetworking only on a presented controller
Asked Answered
D

1

6

I am using AFNetworking 2.0 to monitor 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;
    }

}];

On top of this main VC I load different view controllers/paths/navigation controllers and dismiss once they have been used.

Question What I am trying to do is monitor the connection but only when the main VC is displayed. For example, if I load a navigation controller on top of the main VC and the connection is lost I would still get the call to the reachabilityNotReachableAlert.

How can I only monitor when the main VC is displayed on screen, without having to run stopMonitoring and startMonitoring all the time?

I guess I can put the stopMonitoring in the prepareForSegue method and then startMonitoring in the viewDidAppear, is there not an easier way to do this?

Disquietude answered 19/11, 2013 at 14:20 Comment(1)
I think, you forgot breaks :)Cystitis
K
2

Unfortunately no, there isn't an easier way to do this.

However, the plan you mentioned doesn't sound all that bad. You essentially are asking the manager to turn it's notifications off and on, and it needs you to tell it when to do so.

Here's how I do it:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [manager startMonitoring];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [manager stopMonitoring];
}
Kura answered 22/11, 2013 at 23:32 Comment(2)
Thanks, sometimes I over worry about my approach when it often turns out to be the correct way. I have added an update to add in the super calls.Disquietude
That's been my biggest struggle as a programmer too. No worries. Anything you write today can be refactored tomorrow. These days, if I find that my worrying and searching for the perfect solution is hindering a ship date, I go forward as long as my approach works with all relevant scenarios. And good catch about calling super, I updated my sample code.Kura

© 2022 - 2024 — McMap. All rights reserved.