- (IBAction)downAction:(id)sender
{
NSString *urlstring = @"http://free2.macx.cn:81/tools/other2/PaintCode132.dmg";
NSURL *url = [NSURL URLWithString:urlstring];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setRequestMethod:@"GET"];
NSString *fileName = [urlstring lastPathComponent];
//文件下载到本地的路径
NSString *filePath = [NSHomeDirectory() stringByAppendingString:fileName];
//设置下载完成之后文件的存放路径
[request setDownloadDestinationPath:filePath];
//设置进度条视图,显示下载的进度
[request setDownloadProgressDelegate:self.progressView];
//开始异步加载
[request startAsynchronous];
//使用KVO监听精度视图的变化
[self.progressView addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
}
//KVO观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSNumber *progress = [change objectForKey:@"new"];
double value = [progress doubleValue];
self.progressLabel.text = [NSString stringWithFormat:@"下载进度:%0.1f",value];
}