答案 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]];
}];