Question

Is it possible to add a UIProgressView to a NSURLConnection? Is it as simple as getting the length of the data and then setting it to an int then sending that int to my other class to my UIProgressView?

Can someone show me how to do this? Are there any examples or links that will point me in the right direction?

Thanks!!!

Was it helpful?

Solution

In your didReceiveResponse function you could get the total filesize like so -

_totalFileSize = response.expectedContentLength;.

In your didReceiveData function you can then add up to a total bytes received counter -

_receivedDataBytes += [data length];

Now in order to set the progressbar to the correct size you can simply do -

MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize

(either in the didReceiveData function or somewhere else in your code)

Don't forget to add the variables that hold the number of bytes to your class!

Here's how you could implement the delegates in order to update progressview

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    _totalFileSize = response.expectedContentLength;
    responseData = [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   _receivedDataBytes += [data length];
   MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
   [responseData appendData:data];
 }

I hope this helps..

OTHER TIPS

Accepted answer looks too much like this one. https://stackoverflow.com/a/4255630 This is what really caught my eye.

(either in the didReceiveData function or somewhere else in your code)

Don't forget to add the variables that hold the number of bytes to your class!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top