标签:
1 // 2 // ViewController.m 3 // ASIDownloadDemo 4 // 5 // Created by hellovoidworld on 15/1/28. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "ASIHTTPRequest.h" 11 12 @interface ViewController () 13 14 @property(nonatomic, strong) ASIHTTPRequest *request; 15 16 @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 17 18 - (IBAction)startDownloading; 19 20 @end 21 22 @implementation ViewController 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 // Do any additional setup after loading the view, typically from a nib. 27 } 28 29 - (void)dealloc { 30 [self.request clearDelegatesAndCancel]; 31 self.request = nil; 32 } 33 34 - (IBAction)startDownloading { 35 // 1.创建请求 36 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/images/images.zip"]; 37 self.request = [ASIHTTPRequest requestWithURL:url]; 38 39 // 2.设置文件存放路径 40 NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 41 NSString *filePath = [cachePath stringByAppendingPathComponent:@"images.zip"]; 42 self.request.downloadDestinationPath = filePath; 43 44 // 3.设置下载代理 45 // 由于刚好下载代理方法有个setProgress,可以直接使用progressView的setProgress,直接控制进度条 46 self.request.downloadProgressDelegate = self.progressView; 47 48 // 4.支持断点下载 49 self.request.allowResumeForFileDownloads = YES; 50 51 self.request.completionBlock = ^{ 52 NSLog(@"下载完成!"); 53 }; 54 55 // 5.发送请求 56 [self.request startAsynchronous]; 57 } 58 59 #pragma mark - ASIProgressDelegate 60 /** 下载进度代理方法 */ 61 //- (void)setProgress:(float)newProgress { 62 // self.progressView.progress = newProgress; 63 //} 64 65 @end
[iOS 多线程 & 网络 - 2.10] - ASI框架下载文件
标签:
原文地址:http://www.cnblogs.com/hellovoidworld/p/4257694.html