网络数据下载

  1 @interface ViewController ()<NSURLSessionDataDelegate,NSURLSessionDownloadDelegate>
  2 // 显示图片
  3 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  4 
  5 // 显示进度
  6 @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
  7 
  8 // 下载任务的属性
  9 @property (nonatomic, strong) NSURLSessionDownloadTask *task;
 10 
 11 // 网络请求类
 12 @property (nonatomic, strong) NSURLSession *session;
 13 
 14 // 发送请求类
 15 @property (nonatomic, strong) NSURLRequest *request;
 16 
 17 // 用来保存已经下载的数据,供继续下载使用
 18 @property (nonatomic, strong) NSMutableData *data;
 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     // 设置progressView开始值
 28     self.progressView.progress = 0;
 29     
 30 }
 31 
 32 #pragma mark - 开始下载
 33 - (IBAction)startDownload:(id)sender {
 34     
 35     // 下载的url
 36     NSString *urlStr = @"http://hc24.aipai.com/user/396/23170396/4799208/card/25053445/card.mp4?l=a";
 37     NSURL *url = [NSURL URLWithString:urlStr];
 38     
 39     // 初始化请求类
 40     self.request = [NSURLRequest requestWithURL:url];
 41     
 42     // 创建NSURLSession
 43     
 44     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
 45     
 46     self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
 47     // 下载请求任务
 48     self.task = [self.session downloadTaskWithRequest:self.request];
 49     
 50     // 开启
 51     [self.task resume];
 52     
 53 }
 54 
 55 #pragma mark - 暂停
 56 - (IBAction)pauseDownload:(id)sender {
 57     
 58     // 暂停
 59     if (self.task) {
 60         // 暂停并保存之前已经下载的内容
 61         
 62         __weak typeof (self)weakSelf = self;
 63         [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
 64             weakSelf.data = [NSMutableData dataWithData:resumeData];
 65         }];
 66     }
 67     
 68     // 暂停任务
 69     [self.task suspend];
 70 }
 71 
 72 #pragma mark - 继续下载
 73 - (IBAction)continueDownload:(id)sender {
 74     
 75     // 判断当前任务是否有,是发送请求还是处理数据
 76     if (self.task != nil) {
 77         // 说明已经下载,这里要处理的就是数据
 78         self.task = [self.session downloadTaskWithResumeData:self.data];
 79     } else {
 80         // 此时没有下载任何内容,应该重新发送请求下载
 81         self.task = [self.session downloadTaskWithRequest:self.request];
 82     
 83     }
 84     
 85     // 启动任务
 86     [self.task resume];
 87 }
 88 
 89 #pragma mark - 代理方法
 90 // 下载完成走的方法
 91 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
 92 {
 93     // 找到文件夹管理器
 94     
 95     NSFileManager * fileManager = [NSFileManager defaultManager];
 96     // 先找到沙盒
 97     NSArray *urlArray = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
 98     NSLog(@"%@", urlArray);
 99     // 根据沙盒路径获取Document路径
100     NSURL *url = [urlArray objectAtIndex:0];
101     NSLog(@"%@", url);
102     // 根据系统推荐的类型去保存下载的url
103     NSURL *saveUrl = [url URLByAppendingPathComponent:downloadTask.response.suggestedFilename];
104     NSLog(@"%@", saveUrl);
105     // 使用
106     self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:saveUrl]];
107     
108 }
109 
110 - (void)URLSession:(NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
111 {
112     // 下载当前段的数据
113     NSLog(@"bytesWritten = %lld", bytesWritten);
114     // 已经下载的总数据量
115     NSLog(@"totalBytesWritten = %lld", totalBytesWritten);
116     // 表示总进度
117     NSLog(@"totalBytesExpectedToWrite = %lld", totalBytesExpectedToWrite);
118     // 设置progressView的进度值
119     self.progressView.progress = (CGFloat)totalBytesWritten / (CGFloat)totalBytesExpectedToWrite;
120 }
121 
122 - (void)didReceiveMemoryWarning {
123     [super didReceiveMemoryWarning];
124     // Dispose of any resources that can be recreated.
125 }
原文地址:https://www.cnblogs.com/leikun1113/p/5543248.html