0

I am using AFNetworking to make an HTTP request:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    int t=0;
    t++;
} success:^(NSURLSessionTask *task, id responseObject) {
    NSMutableArray * items = [self parseSearchCategoryWithData:responseObject];
    finishBlock(items);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    finishBlock(nil);
}];

I want to get the progress of the url GET but the Progress block is not called. Any idea what can be the problem?

Boryk
  • 36
  • 9
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • Possible duplicate of [How to get download progress in AFNetworking 2.0?](http://stackoverflow.com/questions/19145093/how-to-get-download-progress-in-afnetworking-2-0) – Larme Oct 14 '16 at 14:01

1 Answers1

2

I would recommend using the following template for loading images with progress:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFImageResponseSerializer serializer];

manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

[manager GET:url.absoluteString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

     float progress = downloadProgress.fractionCompleted;

} success:^(NSURLSessionTask *task, id responseObject) {
    UIImage *responseImage = responseObject;

} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(Failed with error: %@", error);
}];

and this template for downloading videos with progress:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        float progress = downloadProgress.fractionCompleted;

    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

        if(error) {
            NSLog(Error %@", error);
        }
        else {
             NSString *path = filePath.relativeString;
        }   
    }
}];

[downloadTask resume];

I hope this helps!

Boryk
  • 36
  • 9