Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking - saving a downloaded file

I am using AFNetworking and can successfully download a file.

At the end of the download, it does not appear in the directory that I set it to be though.

I did some searching and came across a few questions here on SO where it is suggested I use:

[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

But that comes up with an error and it is as far as I can tell not mentioned in their documentation.

the error is :

/Users/Jeff/Documents/Dropbox-01/Dropbox/Xcode Projects/Try Outs - JEFF/testDownload/testDownload/JWKDownloadViewController.m:177:10: No visible @interface for 'AFURLConnectionOperation' declares the selector 'setCompletionBlockWithSuccess:failure:'

Is there an updated line I need to use???

like image 432
jwknz Avatar asked Jan 04 '13 06:01

jwknz


2 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];
like image 55
pavan309 Avatar answered Oct 14 '22 00:10

pavan309


Yes make sure that u have used correct path into NSOutputStream

Add this:

[_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Error: %@", error);
}];
[_operation start];
like image 24
Paresh Navadiya Avatar answered Oct 14 '22 01:10

Paresh Navadiya