Setting up reachability with AFNetworking 2.0

StuartM picture StuartM · Nov 17, 2013 · Viewed 28.5k times · Source

I am trying to setup Reachability using the new 2.0 AFNetworking.

In my AppDelegate I initialise the sharedManager.

// Instantiate Shared Manager
[AFNetworkReachabilityManager sharedManager];

Then in the relevant VC method I check to see if isReachable:

// Double check with logging
if ([[AFNetworkReachabilityManager sharedManager] isReachable]) {
    NSLog(@"IS REACHABILE");
} else {
    NSLog(@"NOT REACHABLE");
}

At present this is not working as expected in the simulator, but I imagine this would need to be tested on device and not simulator.

Question What I would like to do is monitor the connectivity within the VC. So I run the following in the viewDidLoad:

// Start monitoring the internet connection
[[AFNetworkReachabilityManager sharedManager] startMonitoring];

How would I then register for the changes? What is/would be called once the network connection changes I cannot see this from the documentation.

Answer

Gabriele Petronella picture Gabriele Petronella · Nov 17, 2013

As you can read in the AFNetworking read me page

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];

Here's also a link to the official documentation.