2

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.

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];

                                   }

                               }];

    }

}

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?

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

Community
  • 1
  • 1
PhilBot
  • 748
  • 18
  • 85
  • 173

1 Answers1

6

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
}
David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Another possibility is to use a framework such as AFNetworking which handles a lot of the byplay for you, although it can be bloated for really simple usages. – David Berry Mar 21 '14 at 17:07