views:

459

answers:

1

Hi all,

I want to display an UIProgressbar depending on the amount of data downloaded from the server.

One way I implemented is I made a NSURLConnection and set the delegate. The - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response gave me the expectedContentLengh

And in the - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data I am getting the data part by part every time until all the data are downloaded.

But this did not solve my problem.

Is there any other way to do this ?

All your suggestions are appreciated.

Thanks

+2  A: 

If you know the expected content length, just keep a running total of how much you've received so far divided by the total amount you'll get:

// These should be properties of the delegate class
UIProgressView * progressView;
long long expected;
long long gotSoFar; 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    gotSoFar += data.length;
    progressView.progress = gotSofar / expected;
}

If you get type warnings on the division, cast each long long before doing the division.

Ben S