Demasterpl
Demasterpl

Reputation: 2133

Downloading a File with AFNetworking

Trying to write a method for my iPhone program that given a URL address to a file, it would download to the iOS App's Documents Directory.

Following AFNetowrking's Documentation, it seems to work fine except that the filename is always some garbage.

I'm using Xcode 5 with AFNetworking 2.0 added to my project. Here's the code that I have so far:

//#import "AFURLSessionManager.h"

//On load (or wherever):
[self downloadFile:@"http://www.irs.gov/pub/irs-pdf/fw4.pdf"];

-(void)downloadFile:(NSString *)UrlAddress
{ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:UrlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) 
{
    NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
    return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];
} 
completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) 
{
    NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
}

The end result is that the file is successfully downloaded to my documents directory, but with a garbled name:

File downloaded to: file:///Users/myname/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/25188DCA-4277-488E-B08A-4BEC83E59194/Documents/CFNetworkDownload_60NWIf.tmp

The end result I'm expecting:

File downloaded to: file:///Users/myname/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/25188DCA-4277-488E-B08A-4BEC83E59194/Documents/fw4.pdf

I used cocoapods to add AFNetworking to my project: pod 'AFNetworking', "~> 2.0"

Lastly, what do I need to do to get the progress of the download?

Upvotes: 2

Views: 6714

Answers (2)

Tarek Hallak
Tarek Hallak

Reputation: 18470

Simply you can replace this line:

return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];

with:

return [documentsDirectoryPath URLByAppendingPathComponent:@"fw4.pdf"];

since [targetPath lastPathComponent] returns something like CFNetworkDownload_60NWIf.tmp

Upvotes: 0

Demasterpl
Demasterpl

Reputation: 2133

This is the answer I was able to create:

-(void)downloadFile:(NSString *)UrlAddress
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:UrlAddress]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSString *pdfName = @"The_PDF_Name_I_Want.pdf";

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName];
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 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

    NSLog(@"Download = %f", (float)totalBytesRead / totalBytesExpectedToRead);

}];
[operation start];
}

I am open to improvements or suggestions :)

Upvotes: 5

Related Questions