AFNetworking check progress

11,417

Solution 1

The setDownloadProgressBlock method is part of AFURLConnectionOperation, from which AFHTTPRequestOperation inherits - that's why you don't see it in the AFHTTPRequestOperation documentation. The documentation you're after is here:

http://cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/AFURLConnectionOperation.html

Solution 2

refer a following code. this is a some file download using a AFNetworking Code.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blahblah.com/blahblah.mp3"]];
AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [paths objectAtIndex:0] stringByAppendingPathComponent:@"blahblah.mp3"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
    myProgressView.progress = (float)totalBytesRead / totalBytesExpectedToRead;
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSLog(@"downloadComplete!");
}];
[operation start];
Share:
11,417
woutr_be
Author by

woutr_be

Full stack product engineer with a passion for building great, user friendly products. I'm capable of building large front-ends and still be able to switch to working on back-ends with complex code bases.

Updated on June 25, 2022

Comments

  • woutr_be
    woutr_be about 2 years

    I'm using the AFNetworking framework to download files and write them to the local file system.

    But since the files can be quite big I want to add a UIProgressView, but I can't seem to find any method that gets updated with the progress.

    I see people talking about setProgressBlock, but I can't find any information about this in the docs: http://afnetworking.org/Documentation/Classes/AFHTTPRequestOperation.html

    Is there a method that does this? I'm just using AFHTTPRequestOperation to download the files.