标签:
1 #import "ViewController.h" 2 3 @interface ViewController () <NSURLSessionDataDelegate, NSURLSessionDownloadDelegate> 4 5 @property (weak, nonatomic) IBOutlet UIImageView *imgView; 6 @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 7 8 9 /** 下载任务的属性 */ 10 @property (nonatomic, strong) NSURLSessionDownloadTask *task; 11 12 /** 网络请求类 */ 13 @property (nonatomic, strong) NSURLSession *session; 14 15 /** 发送请求类 */ 16 @property (nonatomic, strong) NSURLRequest *request; 17 18 /** 保存已经下载的数据,供继续下载使用 */ 19 @property (nonatomic, strong) NSMutableData *data; 20 21 22 @end 23 24 @implementation ViewController 25 26 - (void)viewDidLoad { 27 [super viewDidLoad]; 28 29 // 设置progress的初始值 30 self.progressView.progress = 0; 31 } 32 33 34 #pragma mark - 开始下载 35 - (IBAction)start:(id)sender { 36 37 // 下载的url 38 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"]; 39 40 // 初始化请求类 41 self.request = [NSURLRequest requestWithURL:url]; 42 43 // 创建NSURLSession 44 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 45 self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 46 47 // 下载请求任务 48 self.task = [self.session downloadTaskWithRequest:self.request]; 49 50 // 开启 51 [self.task resume]; 52 } 53 54 55 #pragma mark - 暂停下载 56 - (IBAction)pause:(id)sender { 57 58 if (self.task) { 59 60 __weak typeof(self)weakSelf = self; 61 // 暂停并保存之前已经下载的内容 62 [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) { 63 64 weakSelf.data = [NSMutableData dataWithData:resumeData]; 65 }]; 66 } 67 68 // 暂停任务 69 [self.task suspend]; 70 } 71 72 73 #pragma mark - 继续下载 74 - (IBAction)continue:(id)sender { 75 76 // 判断当前有没有任务,是发送请求,还是处理数据 77 if (self.task != nil) { 78 79 // 说明已经下载,这里要处理的就是数据 80 self.task = [self.session downloadTaskWithResumeData:self.data]; 81 } else { 82 83 // 此时没有下载任何内容,应该重新发送请求进行下载 84 self.task = [self.session downloadTaskWithRequest:self.request]; 85 } 86 87 // 启动 88 [self.task resume]; 89 } 90 91 92 #pragma mark - 代理方法 93 // 下载完成的方法 94 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { 95 96 NSFileManager *fileManager = [NSFileManager defaultManager]; 97 NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:downloadTask.response.suggestedFilename]; 98 [fileManager moveItemAtPath:location.path toPath:path error:nil]; 99 self.imgView.image = [UIImage imageWithContentsOfFile:path]; 100 } 101 102 103 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { 104 105 // 下载当前段的数据 106 NSLog(@"bytesWritten = %lld", bytesWritten); 107 108 // 已经下载的总数据量 109 NSLog(@"totalBytesWritten = %lld", totalBytesWritten); 110 111 // 总进度 112 NSLog(@"totalBytesExpectedToWrite = %lld", totalBytesExpectedToWrite); 113 114 // 设置progress的进度值 115 self.progressView.progress = (CGFloat)totalBytesWritten / (CGFloat)totalBytesExpectedToWrite; 116 } 117 118 @end
标签:
原文地址:http://www.cnblogs.com/zhizunbao/p/5543865.html