***Now with updates*** I have been getting a lot of people hitting my blog recently having searched for *"AFNetworking resume download"*. ASIHTTPRequest supported partial downloads out of the box - for developers writing apps that involved consuming large (50MB+) bundles of content this was extremely useful, particularly in the days before background tasks were supported on iOS. AFNetworking is a lightweight library, and partial downloading isn't supported as such. Hopefully this post can help those looking to implement such functionality on their own. Firstly, consider whether you *need* partial downloading. AFNetworking can download in the background on iOS 4.0 and up, and this may be good enough for your needs. [As per this StackOverflow answer, all you need to do is add this code][1] (or similar): - (void)applicationWillResignActive:(UIApplication *)application { [application beginBackgroundTaskWithExpirationHandler:^(void) { [[YourRestClient sharedClient] cancelAllHTTPOperations]; }]; } Should you require partial downloading you are going to have to implement it yourself. Fortunately, it's not too complex! ## Getting Prepared Partial downloading can only be used with servers that support it, so do check your hosting settings to make sure this is the case for you. If you're trying to download from a server you don't manage or own you can check for the `Accept-Ranges` HTTP header (note that this header *isn't* mandatory, so in some cases you might encounter a server that supports ranged downloads but doesn't return the `Accept-Ranges` header). ## How it works In case you're not familiar with how ranged downloads work, here's a quick summary: by setting the `range` HTTP header we can ask for only a certain portion of the file being requested to be sent. Here are two examples: Range: bytes=500-999 Range: bytes=300- In the first example we're requesting from 500 through 999 bytes *inclusive*. The second example simply requests from 300 bytes until the end of the file. This is useful if you're unsure of the total file size. ## The Magic Hopefully by now you should have a pretty good understanding of how ranged downloads work. Implementing it in your code is surprisingly straightforward. You'll need to: * Create an `NSOutputStream` and make sure your AFNetworkingOperation's `outputStream` points to it. You'll probably want your output stream to save to a local file. * When your `AFNetworkingOperation` is cancelled (or finishes!), tidy up and close your output stream. You can use the standard `NSURLConnection` delegate methods to handle this. * At some point in the future, create a new request pointing to the same URL, and set the `Range` header to start from where you left off (this is simply the size of your partially completed file in bytes). It is admittedly easier to say this than to actually *do* it, but the amount of code involved is limited. If I have some time later this week I will try to actually get some sample code together that allows for partial downloading using `AFNetworking`. **17/3/12 - Update:** ========= A few weeks after I posted this Evan Rosenfield implemented resumable downloading in his `AFNetworking` fork, [which you can find on GitHub here][2], which gives you a good sense of what's needed. Of course, there's no guarantee it stays up to date with the main `AFNetworking` repository, so do proceed with caution if you're using it in your own projects. [1]: http://stackoverflow.com/questions/7800614/does-afnetworking-have-backgrounding-support [2]:https://github.com/emrosenf/AFNetworking