0

I have a simple NSURLConnection I want to get the parsing data progress percentage. That is how much percentage loaded. Is it possible to calculate. I have tried this,

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    urlData = [NSMutableData data];
    [urlData setLength:0];
  expectedBytes = [response expectedContentLength];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
float progressive = (float)[urlData length] / (float)expectedBytes;
}

here, expectedBytes getting -1. The json link is working getting jsonResult. Let me know how to Calculate the loading progress.

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • It has to be sent from server to determine the expected length. Perhaps [this](http://stackoverflow.com/questions/7417610/why-response-expectedcontentlength-always-return-1) would help you. – GoodSp33d Feb 20 '15 at 04:35

1 Answers1

2

Use below code. This will work for you,Simple and easy.

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{
      urlData = [NSMutableData data];
      [urlData setLength:0];
      double expectedBytes = [[[response allHeaderFields] objectForKey:@"Content-Length"] doubleValue];
    }

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
      float progressive = (float)[urlData length] / (float)expectedBytes;
}
Nirmal Choudhari
  • 559
  • 5
  • 17