1

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!!!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

2 Answers2

3

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..

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • Wow thanks! Just one thing, could you show me how I could set these values to a UIProgressView in another view controller? – SimplyKiwi Dec 05 '11 at 12:04
  • for that you need to implement `delegate` methods. This is a standard practice in ios to share data or info between view controllers. – Srikar Appalaraju Dec 05 '11 at 12:19
  • Is that the easiest way to go upon doing it? If I had to use delegate methods how would I use it in this case? – SimplyKiwi Dec 05 '11 at 12:23
  • not sure about the easiest way. But this is the recommended good practice while developing apps in ios using objectiveC. for more check this - http://stackoverflow.com/questions/2534094/what-is-a-delegate-in-objective-cs-iphone-development & http://stackoverflow.com/questions/1045803/how-does-a-delegate-work-in-objective-c – Srikar Appalaraju Dec 05 '11 at 12:33
  • Oh I see! Hmm I think I need to keep my delegate callbacks in the view controller they are in since a lot of things go on there that can't be moved. Is there any other way I do this maybe by using properties? – SimplyKiwi Dec 05 '11 at 12:39
  • well you could do it that way. have some class vars & some member functions for a particular view controller & use them to pass data between 2 or more view controllers. basically init one view controller inside another etc. IF that solves your purpose who is to say its not the right method... – Srikar Appalaraju Dec 05 '11 at 12:43
  • Ok so how would I do that? Like this? MyClass *class = [[[MyClass alloc] init] autorelease]; class.myprogressBar.progress = _receivedDataBytes / (float)_totalFileSize; – SimplyKiwi Dec 05 '11 at 20:56
0

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!

Community
  • 1
  • 1
GauravY
  • 78
  • 5
  • It really quite does, haha. People are hijacking my answer, I'm flattered <:o). Oh well, as long as it serves the purpose of helping OP. – Hless Jul 08 '14 at 10:16