简体   繁体   English

带有sendAsynchronousRequest Objective-C的NSURLConnection进度栏

[英]NSURLConnection Progress Bar with sendAsynchronousRequest Objective-C

I am downloading a bunch of largish zip files with the following method. 我正在使用以下方法下载一堆较大的zip文件。 It can take a little while and so I'd like to display a progress bar. 可能要花一点时间,所以我想显示一个进度条。

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". 我已经研究了如何与NSURLConnection的委托方法一起使用,这似乎很简单,但是我想通过“ 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];

                                   }

                               }];

    }

}

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDownloadDelegate_Protocol/NSURLConnectionDownloadDelegate/NSURLConnectionDownloadDelegate.html#//apple_ref/doc/uid/TP40010954-CH2-SW1 https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDownloadDelegate_Protocol/NSURLConnectionDownloadDelegate/NSURLConnectionDownloadDelegate.html#//apple_ref/doc/uid/TP40010954-CH2-SW1

How to make an progress bar for an NSURLConnection when downloading a file? 下载文件时如何为NSURLConnection制作进度条?

http://iphonedevsdk.com/forum/iphone-sdk-development/24233-nsurlconnection-with-uiprogressbar.html 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 http://iphoneeasydevelopment.blogspot.com/2011/10/use-progess-bar-when-downloading-file.html

sendAsynchronousRequest won't work for your purposes as it doesn't call your callback until the request has completed. sendAsynchronousRequest不能满足您的目的,因为它在请求完成之前不会调用您的回调。 You'll need to use initRequest:withDelegate: and handle your own data accumulation. 您将需要使用initRequest:withDelegate:并处理您自己的数据累积。

When the header is received (possibly multiple times for redirects) your didReceiveResponse method will be called, you can pick up the expected size there: 收到标头时(可能多次重定向),将调用didReceiveResponse方法,您可以在此处选择所需的大小:

-(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. 每次接收到大块数据时,您都会收到对委托方法didReceiveData的调用,因此您知道到目前为止已接收了多少数据。

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

   // make a progress update here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM