I've implemented AFNetworking
without subclassing AFHTTPClient
, in part using the following code in my DownloadQueueManager
:
-(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 importing SystemConfiguration.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.
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
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With