AFNetworking checking Availability AFNetworking checking Availability ios ios

AFNetworking checking Availability


Actually contrary to what A-Live said Reachability IS a part of AFNetworking. It's implemented in AFHTTPClient.h here. You need the correct imports in your .pch file as discussed here in order to use it.

To use it you'll probably want to have a subclass of AFHTTPClient so you can use setReachabilityStatusChangeBlock defined here. Here's a simple example without using a subclass.

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://google.com"]];[client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {    if (status == AFNetworkReachabilityStatusNotReachable) {        // Not reachable    } else {        // Reachable    }    if (status == AFNetworkReachabilityStatusReachableViaWiFi) {        // On wifi    }}];

If you don't like how this reachability setup works then I would recommend Tony Million's fork of Apple's Reachability. Simple example:

Reachability *reach = [Reachability reachabilityWithHostname:@"google.com"];if ([reach isReachable]) {    // Reachable    if ([reach isReachableViaWiFi]) {        // On WiFi    }} else {    // Isn't reachable    [reach setReachableBlock:^(Reachability *reachblock)    {        // Now reachable    }];    [reach setUnreachableBlock:^(Reachability*reach)    {        // Now unreachable    }];}


With AFNetworking 2.0 and above, one can check for availability like this,

    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {    switch (status) {        case AFNetworkReachabilityStatusUnknown:        case AFNetworkReachabilityStatusReachableViaWWAN:        case AFNetworkReachabilityStatusReachableViaWiFi:            //available            break;        case AFNetworkReachabilityStatusNotReachable:            //not available            break;        default:            break;    }    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));}];//start monitoring[[AFNetworkReachabilityManager sharedManager] startMonitoring];

To get current status

[AFNetworkReachabilityManager sharedManager].reachable


Just an update, the newer version of AFNetworking has deprecated AFHTTPClient.

You can use AFHTTPRequestOperationManager.h instead

Something small taken from the github page itself:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url]; //url can be google.com or something you want to reachNSOperationQueue *operationQueue = manager.operationQueue;[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){    switch (status)    {        case AFNetworkReachabilityStatusReachableViaWWAN:        case AFNetworkReachabilityStatusReachableViaWiFi:        {            NSLog(@"SO REACHABLE");            [operationQueue setSuspended:NO]; // or do whatever you want            break;        }        case AFNetworkReachabilityStatusNotReachable:        default:        {            NSLog(@"SO UNREACHABLE");            [operationQueue setSuspended:YES];             //not reachable,inform user perhaps            break;        }    }}];[manager.reachabilityManager startMonitoring];