AFNetworking checking Availability
Solution 1
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
}];
}
Solution 2
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
Solution 3
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 reach
NSOperationQueue *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];
AMayes
Updated on July 05, 2022Comments
-
AMayes almost 2 years
I've implemented
AFNetworking
without subclassingAFHTTPClient
, in part using the following code in myDownloadQueueManager
:-(void)downloadPodcastAt:(NSString *)url toPath:(NSString *)path { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { [self saveQueuedItemInformation]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Other stuff }]; [operation start]; }
My question is manifold. I've googled til' my fingers went numb, and have yet to find a decent code sample that simply and easily checks for Reachability status using
AFNetworking
. (Oddly, there is plenty of discussion about importingSystemConfiguration.framework
, which seems like a no-brainer). So if my user wants to minimize their data usage, and only download using wifi, how do I check for wifi, and only download if wifi is available?Second, it seems like
AFNetworking
wants to be a user-friendly front-end. But I could actually use a front-end to this front-end, because there's a LOT of stuff in there that one has to weed through to get to the stuff one needs. I just need to access a url, download an xml file (based on reachability), and do stuff with it. Am I missing something that makes this a simple task ?When I make sense of this, I'm totally building a front-end or five to simplify implementation (assuming I'm not just an idiot). Thanks in advance for any responses.