Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking - Using AFHTTPRequestOperation to download file?

Tags:

afnetworking

Man, stumped on this one. Trying to download a file with AFNetworking and the best I can do is capture the progress bytes in the downloadProgressBlock. I've tried many different forks of AFNetworking and now I'm back to the latest build. Looks like at one time the AFHTTPRequestOperation was modified to also include NSURLResponse but that's gone in the latest release. And, per the code below, the "success" block is never called. I get a log of the downloaded bytes and then ^complete is called. success and error are never called.

Any guidance on this would be awesome. I can't figure out where the data is returned and how to get it to use NSFileManager on it? I need to download files, not write streams for images, etc.

Edit: I've also tried to capture the data by overriding - (void)connection:didReceiveData as suggested in the documentation but nothing doing there.

// url is http://mydomain.com/somezip.zip

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", url]]];

AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:request success:^(id object) {

    NSLog(@"hey, some success!");

} failure:^(NSHTTPURLResponse *response, NSError *error) {

    NSLog(@"%@", error);

}];


[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {

    // callback is sent in and works great.  Just outputting to the console at the moment
    SEL callme = NSSelectorFromString(callback);
    [sender performSelectorOnMainThread:callme withObject:[NSNumber numberWithInt:bytesRead] waitUntilDone:NO];

}];

[operation setCompletionBlock:^{
    NSLog(@"operation complete");
}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
like image 317
Bat Lanyard Avatar asked Nov 04 '11 04:11

Bat Lanyard


1 Answers

You can simply use this feature that is included on AFNetworking and even save it to disk - Notice the operation.outputStream:

NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE" parameters:nil];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease];

NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

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

    NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path)

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // Deal with failure
}];

EDIT : missed

[operation start]; 

EDIT : ...or if you use an AFHTTPClient * apiClient :

// [apiClient enqueueHTTPRequestOperation:operation];
like image 141
Carlos Ricardo Avatar answered Oct 10 '22 00:10

Carlos Ricardo