2

I am trying to show the Progress Bar as I download a JSON from a url. The JSON is downloading correctly but I am not sure how to show the Progress Bar. I have tried using UIProgressView but it does not display on the screen. Any suggestions would be appreciated.

CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;

CGRect rect = CGRectMake(width/2, height/2, width/5, height/5);
UIProgressView *myProgressView = [[UIProgressView alloc]initWithFrame:rect];
myProgressView.progressViewStyle = UIProgressViewStyleBar;
[self.view addSubview:myProgressView];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];    
[manager GET:@"https:urlWithJson" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
      [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
       {
           myProgressView.progress = (float)totalBytesRead / totalBytesExpectedToRead;
       }];
      [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSLog(@"operation Completed");
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"ERROR");
      }];

      [operation start];
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"timeout Error: %@", error);
  }];
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
baskInEminence
  • 710
  • 11
  • 26
  • Does totalBytesExpectedToRead have a value? The HTTP header must contain the size of the document in order to get the total expected bytes. – Thomas Jun 28 '15 at 09:37
  • I tried putting in a NSLog statement inside setDownloadProgressBlock but it did not print anything. Is there a better way I can achieve what I want? – baskInEminence Jun 28 '15 at 10:17

2 Answers2

5

You set the download-progress-block inside the success block, which is a bit too late ;)

Try this:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];    
AFHTTPRequestOperation *operation = [manager GET:@"https:urlWithJson" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Complete");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    myProgressView.progress = (float)totalBytesRead / totalBytesExpectedToRead;
}];
Thomas
  • 2,338
  • 2
  • 23
  • 32
2

Simple solution for Swift:

let progress: UIProgressView? //for example, reference to IBOutlet
let manager = AFHTTPRequestOperationManager()
let operation = manager.POST(query, parameters: parameters, constructingBodyWithBlock: { multipartData in

             //do sth with your multipartData
    }, success: { operation, response in

            //success
    }) { operation, error in

            //failure
}

progress?.setProgressWithUploadProgressOfOperation(operation!, animated: true)

You do nothing more. This is enough to show progress on your view.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358