Add UIProgressView to a NSURLConnection?

巧了我就是萌 提交于 2019-12-08 06:49:46

问题


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


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/8382075/add-uiprogressview-to-a-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!