NSURLConnection进度条,带有sendAsynchronousRequest Objective-C [英] NSURLConnection Progress Bar with sendAsynchronousRequest Objective-C
问题描述
我使用以下方法下载一堆大量的zip文件。它可能需要一段时间,所以我想显示一个进度条。
I am downloading a bunch of largish zip files with the following method. It can take a little while and so I'd like to display a progress bar.
我研究了如何使用NSURLConnection的委托方法,它似乎直接,但是我想用sendAsynchronousRequest实现同样的事情。如何获取下载的字节数以及预期的总字节数,以便我可以显示进度条?我理解,如果我以我的方式开始下载,我不能使用委托方法。
I've researched how to do with with the delegate methods for NSURLConnection and it seems straightforward, however I want to achieve the same thing with "sendAsynchronousRequest". How can I get the number of bytes downloaded as it downloads as well as the total number of bytes expected so that I can display a progress bar? I understand that I cannot use the delegate methods if I kick off a download in the manner I am doing it.
// Begin the download process
- (void)beginDownload:(NSMutableArray *)requests {
// Now fire off a bunch of requests asynchrounously to download
self.outstandingRequests = [requests count];
for (NSURLRequest *request in requests) { // Get the request
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// Error check
if ( error != nil ) {
// The alertview for login failed
self.appDelegate.warningView.title = @"Refresh Error!";
self.appDelegate.warningView.message = [error localizedDescription];
// Show the view
[self.appDelegate.warningView show];
// Debug
if ( DEBUG ) {
NSLog(@"A request failed - %d left!",self.outstandingRequests);
}
}
else {
// Debug
if ( DEBUG ) {
NSLog(@"A request is done - %d left!",self.outstandingRequests);
}
}
// Decrement outstanding requests
self.outstandingRequests--;
// No requests are left
if (self.outstandingRequests == 0) {
// Debug
if ( DEBUG ) {
NSLog(@"All requests are done!");
}
// Get rid of loading view
[self performSelector:@selector(dismissLoadingView) withObject:nil afterDelay:0.15];
}
}];
}
}
如何在下载文件时为NSURLConnection创建进度条?
http://iphonedevsdk.com/forum/iphone-sdk-development/24233-nsurlconnection-with-uiprogressbar.html
http://iphoneeasydevelopment.blogspot.com/2011/10/use-progess-bar-when-downloading-file.html
推荐答案
sendAsynchronousRequest不会为您的目的工作,因为它不会调用您的回调,直到请求完成。你需要使用initRequest:withDelegate:和处理你自己的数据积累。
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.
当接收到标题(可能多次重定向)时,您的didReceiveResponse方法将被调用,您可以选择预期的大小:
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
}
每次接收到一个数据块时,都会收到对委托方法didReceiveData的调用,因此您知道到目前为止接收了多少数据。
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
}
这篇关于NSURLConnection进度条,带有sendAsynchronousRequest Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!