如何在后台下载多个文件并更新相应的进度视图

时间:2014-02-28 11:53:11

标签: asynchronous ios7 background nsurlconnection nsurlsession

我正在使用NSURLSession在后台下载文件..我看过this

链接,它工作正常。但我必须下载多个文件,就像我上传的图像。

如何实现这一点,任何建议都将受到高度赞赏。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用分组编码

  typedef void (^onDownload)(NSData *data);
typedef void (^onDownloadProgress)(id,id);

@property (nonatomic,retain) onDownload block;
@property (nonatomic,retain) onDownloadProgress progressBlock;

<强>下载

 -(void) downloadFileFromURL:(NSURL *)url1 withCompletionBlock:(onDownload)completeBlock withProgressBlock:(onDownloadProgress) pBlock{
    self.block = [completeBlock copy];
    self.progressBlock = [pBlock copy];
    self.url = url1;
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:url1] delegate:self];
    [connection start];
}

 -(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
            float progress = (totalBytesWritten *100)/totalBytesExpectedToWrite;
             self.progressBlock(self,[NSNumber numberWithDouble:progress]);
            //return process
    }

请求网址

FileDownloader *download=[[FileDownloader alloc]init];

    [download downloadFileFromURL:url withCompletionBlock:^(NSData *data) {
        [lblProcess setHidden:YES];

      //Compale Dwonloading

    } withProgressBlock:^(FileDownloader *download,NSNumber* process) {
        lblProcess.text=[NSString stringWithFormat:@"%.0f%%",[process doubleValue]];
    }];