Add NSURLConnection loading process on UIProgressView

假装没事ソ 提交于 2019-11-27 19:56:22

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!

I hope this helps..

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