Setting up reachability with AFNetworking 2.0 Setting up reachability with AFNetworking 2.0 ios ios

Setting up reachability with AFNetworking 2.0


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.


I have a singleton AFHTTPRequestOperationManager class. In the singleton has a method:

+(void)connectedCompletionBlock:(void(^)(BOOL connected))block {[[AFNetworkReachabilityManager sharedManager] startMonitoring];[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {    BOOL con = NO;    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));    if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {        con = YES;    }    if (block) {        [[AFNetworkReachabilityManager sharedManager] stopMonitoring];        block(con);    }}];

}

Before make a request you call this method that return a block indicating if internet is reachable:

[TLPRequestManager connectedCompletionBlock:^(BOOL connected) {    if (connected) {       // Make a request    }else{        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notice" message:@"Internet is not available." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];        [alertView show];    }}];


I was just going through your question and all the answers. After that I decided to do all these things once. So, in my existing project I just included the AFNetworking through cocoa-pods and here is the solution which is woking for me completely.

Solution -- First of all AFNetworkReachabilityManager is a singleton class. You don't need to do AppDelegate initialisation for sharedManager.

//[AFNetworkReachabilityManager sharedManager];#import <AFNetworkReachabilityManager.h>- (void)viewDidLoad {//Starting the network monitoring process[[AFNetworkReachabilityManager sharedManager]startMonitoring];//Checking the Internet connection...[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){    if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {        UIAlertView *alertNetFound = [[UIAlertView alloc]initWithTitle:@"Network Found" message:@"Please Wait Until It is loading" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alertNetFound show];    }else{        UIAlertView *alertNetNotFound = [[UIAlertView alloc]initWithTitle:@"No Internet" message:@"Please Check Your Internet Connection Honey" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alertNetNotFound show];    }}];

So, in this case every time the device connects to a network, it will do the startMonitoring process first and after that it will hit the status block every time and will display alert according to the status.

You can do anything according to your choice by replacing the alerts on the status block. I used this to load an webpage automatically from local storage but I removed that code for simplicity.

Its even working with my simulator and Mac mini..

Thanks

Hope this helped.