首页 > 移动平台 > 详细

ios-ASIHTTPRequest下载与进度条以及用观察者模式来监听进度条属性

时间:2014-04-15 23:01:42      阅读:834      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
UIProgressView *uiprogressview= [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];//建立一个进度条
    uiprogressview.frame=CGRectMake(50, 100, 200, 20);
    uiprogressview.tag=10;
    [self.window addSubview:uiprogressview];
    //通过观察者模式来检测这个属性
    [uiprogressview addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
    NSString * fileurlstring=@"http://free2.macx.cn:81/tools/system/CleanMyMac-v1-10-8.dmg";
    NSString *downloadpath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filename=[fileurlstring lastPathComponent];
    NSString *downloadpath_now=  [downloadpath stringByAppendingPathComponent:filename];
    ASIHTTPRequest*request= [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:fileurlstring]];
    [request setDownloadDestinationPath:downloadpath_now];//设置存储路径
    [request setDownloadProgressDelegate:[self.window viewWithTag:10]];//绑定一个进度条
    [request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {
        NSLog(@"%@",responseHeaders);//里面包含了数据大小的那些内容
    }];//当服务器做出响应后调用的block,返回的一个响应头,同理有请求头
    [request startAsynchronous];//启动异步下载


bubuko.com,布布扣
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    NSLog(@"%@",keyPath);
  NSNumber *nuber=  [change objectForKey:@"new"];
  float ff=  [nuber floatValue];
    NSLog(@"%.1f%%",ff*100);
    
}
bubuko.com,布布扣

 

 
bubuko.com,布布扣

 

ios-ASIHTTPRequest下载与进度条以及用观察者模式来监听进度条属性,布布扣,bubuko.com

ios-ASIHTTPRequest下载与进度条以及用观察者模式来监听进度条属性

原文:http://www.cnblogs.com/liyang31tg/p/3664945.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!