ios - 带有 sendAsynchronousRequest Objective-C 的 NSURLConnection 进度条

标签 ios iphone objective-c cocoa nsurlconnection

我正在使用以下方法下载一堆较大的 zip 文件。这可能需要一些时间,所以我想显示一个进度条。

我研究了如何处理 NSURLConnection 的委托(delegate)方法,这看起来很简单,但是我想用“sendAsynchronousRequest”实现同样的事情。如何获取下载时下载的字节数以及预期的总字节数,以便显示进度条?我了解,如果我以我正在执行的方式开始下载,我将无法使用委托(delegate)方法。

// 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

最佳答案

sendAsynchronousRequest 不会为您的目的工作,因为在请求完成之前它不会调用您的回调。您需要使用 initRequest:withDelegate: 并处理您自己的数据积累。

当接收到 header 时(可能多次重定向)您的 didReceiveResponse 方法将被调用,您可以在那里选择预期的大小:

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

   // make a progress update here
}

每次收到数据 block 时,您都会收到对委托(delegate)方法 didReceiveData 的调用,因此您知道到目前为止收到了多少数据。

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

   // make a progress update here
}

关于ios - 带有 sendAsynchronousRequest Objective-C 的 NSURLConnection 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22564582/

相关文章:

ios - Base64解码

ios - NSFetchedResultsController 使用错误的更新类型进行更新

iphone - iOS 键盘位置和方向

ios - Xcode 6 iPair Air 无法在设备上运行应用程序(操作系统版本)

objective-c - AVAssetExportSession 给出 -620 错​​误(物理内存不足),有 4 GB 可用 RAM

ios - 如何在 init 中将参数从 Objective C 传递到 Swift

iphone - 我需要在仪器上测试我的应用程序吗?

ios - iPhone : Multiple touches on a UIButton are not recognized

iphone - 在 iphone safari webapp 中隐藏键盘

iphone - UIImage 调整大小而不损失质量