I am using the AFNetworking library. I can't figure out how to download a file and save it to the documents directory.
8 Answers
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"..."]];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"filename"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];

- 19,544
- 7
- 73
- 84
-
10You can even add a progress block: //Setup Upload block to return progress of file upload [operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToRead) { float progress = totalBytesWritten / (float)totalBytesExpectedToRead; NSLog(@"Download Percentage: %f %%", progress*100); }]; – Climbatize Nov 29 '12 at 13:41
-
19Be careful that the above code will download any response from the server to the output stream, so if the server responds with status code 404, the 404 page will be saved to the path specified. You must check on the success block for operation.response.statusCode. – leolobato Oct 15 '13 at 19:30
-
6Instead of manually checking the status code, just use `operation.hasAcceptableStatusCode` – Bob Spryn Sep 02 '14 at 15:23
-
`NSURLRequest` How to post or get one url? – Gank Nov 25 '14 at 09:42
-
how to download only in success block ? @leolobato can you advise please? – Khant Thu Linn Feb 17 '16 at 02:57
-
@leolobato No need, [AFNetworking takes care of this for you](https://github.com/AFNetworking/AFNetworking/issues/137). – Ryan Brodie Mar 25 '16 at 23:56
-
when i download video and click on back witho download completion. and again i click on video that time i get this error =>MediaPlayerErrorDomain Code=-11800 ... so plz help me – ramesh bhuja Jun 13 '16 at 12:21
-
how to do this with POST with parameters? – ios developer Aug 26 '16 at 11:17
-
how to call Get Type method with some header data and download a file – DURGESH Mar 01 '17 at 06:40
I'm gonna bounce off @mattt's answer and post a version for AFNetworking 2.0 using AFHTTPRequestOperationManager
.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"filename"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *op = [manager GET:@"http://example.com/file/to/download"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"successful download to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
op.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

- 48,060
- 27
- 100
- 130
-
1I would definitely recommend doing it this way with the newer AFNetworking library – Thomas May 28 '14 at 14:17
-
2@swilliams [AFHTTPRequestOperationManager manager] will create new manager object using default AFJSONResponseSerializer, do you want that? I think it 's better to create AFNoneResponseSerialize to let the file untouched – onmyway133 Jun 11 '14 at 10:08
-
-
-
Is there a way to have the file cleaned up automatically if the download does not complete? – lostintranslation Jul 06 '15 at 15:41
-
when i download video and click on back witho download completion. and again i click on video that time i get this error =>MediaPlayerErrorDomain Code=-11800 ... so plz help me – ramesh bhuja Jun 13 '16 at 12:21
I'm talking about AFNetworking 2.0
[AFHTTPRequestOperationManager manager]
creates manager object with default AFJSONResponseSerializer, and it performs content types restriction. Take a look at this
- (BOOL)validateResponse:(NSHTTPURLResponse *)response
data:(NSData *)data
error:(NSError * __autoreleasing *)error
So we need to create a none response serializer and use AFHTTPRequestOperationManager
as normal.
Here is the AFNoneResponseSerializer
@interface AFNoneResponseSerializer : AFHTTPResponseSerializer
+ (instancetype)serializer;
@end
@implementation AFNoneResponseSerializer
#pragma mark - Initialization
+ (instancetype)serializer
{
return [[self alloc] init];
}
- (instancetype)init
{
self = [super init];
return self;
}
#pragma mark - AFURLResponseSerializer
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error
{
return data;
}
@end
Usage
self.manager = [AFHTTPRequestOperationManager manager];
self.manager.responseSerializer = [AFNoneResponseSerializer serializer];
[self.manager GET:@"https://sites.google.com/site/iphonesdktutorials/xml/Books.xml"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
if (success) {
success(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
so that we can get the whole file without any serialization

- 45,645
- 31
- 257
- 263
Documentation page has example with section 'Creating a Download Task':
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
NB! Code work with iOS 7+ (tested with AFNetworking 2.5.1)

- 4,463
- 2
- 37
- 82
-
when i download video and click on back witho download completion. and again i click on video that time i get this error =>MediaPlayerErrorDomain Code=-11800 ... so plz help me – ramesh bhuja Jun 13 '16 at 12:21
From AFNetworking docs. Save to loaded file to your documents. AFNetworking 3.0
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];

- 1,105
- 1
- 11
- 15
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFCompoundResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/octet-stream"];
AFHTTPRequestOperation *operation = [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
// your code here
} else {
// your code here
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[operation start];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject: @"application/octet-stream"]; can vary depending on what you expect

- 4,463
- 2
- 37
- 82

- 465
- 6
- 20
Yes, it is better to use AFNetworking 2.0
way with AFHTTPRequestOperationManager
. With old way my file did download but for some reason didn't update in file system.
Appending to swilliam's answer, to show download progress, in AFNetworking 2.0
you do similarly - just set download progress block after setting output stream.
__weak SettingsTableViewController *weakSelf = self;
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:newFilePath append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToRead) {
float progress = totalBytesWritten / (float)totalBytesExpectedToRead;
NSString *progressMessage = [NSString stringWithFormat:@"%@ \n %.2f %% \n %@ / %@", @"Downloading ...", progress * 100, [weakSelf fileSizeStringWithSize:totalBytesWritten], [weakSelf fileSizeStringWithSize:totalBytesExpectedToRead]];
[SVProgressHUD showProgress:progress status:progressMessage];
}];
This is my method to create bytes string:
- (NSString *)fileSizeStringWithSize:(long long)size
{
NSString *sizeString;
CGFloat f;
if (size < 1024) {
sizeString = [NSString stringWithFormat:@"%d %@", (int)size, @"bytes"];
}
else if ((size >= 1024)&&(size < (1024*1024))) {
f = size / 1024.0f;
sizeString = [NSString stringWithFormat:@"%.0f %@", f, @"Kb"];
}
else if (size >= (1024*1024)) {
f = size / (1024.0f*1024.0f);
sizeString = [NSString stringWithFormat:@"%.0f %@", f, @"Mb"];
}
return sizeString;
}

- 15,320
- 6
- 84
- 70
-
when i download video and click on back witho download completion. and again i click on video that time i get this error =>MediaPlayerErrorDomain Code=-11800 ... so plz help me – ramesh bhuja Jun 13 '16 at 12:22
In addition to the previous answers, with AFNetworking 2.5.0 and iOS7/8 I have found that that the extra step of opening the output stream is also needed to prevent the app from hanging (and eventually crashing from lack of memory).
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:dest
append:NO];
[operation.outputStream open];
[operation start];

- 192
- 1
- 10