NSURLConnection Progress Bar with sendAsynchronousRequest Objective-C

穿精又带淫゛_ 提交于 2019-12-04 15:13:18

sendAsynchronousRequest won't work for your purposes as it doesn't call your callback until the request has completed. You'll need to use initRequest:withDelegate: and handle your own data accumulation.

When the header is received (possibly multiple times for redirects) your didReceiveResponse method will be called, you can pick up the expected size there:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _expectedBytes = (NSUInteger)response.expectedContentLength;
    _data = [NSMutableData dataWithCapacity:_expectedBytes];

   // make a progress update here
}

You'll receive a call to the delegate method didReceiveData each time a chunk of data is received, so you know how much data you've received up to this point.

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_data appendData:data];
    _receivedBytes = _data.length;

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