![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
文件下载一(不支持断点续传)
这种下载方式用的场合比较少,逻辑比较复杂,代码量也比较大。
主要的处理在两个代理方法中
- -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
- if (self.currentLength) return;
- //文件路径
- NSString * caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
- NSString * filepath = [caches stringByAppendingPathComponent:@"file.zip"];
- NSLog(@"%@",filepath);
- _path = filepath;
- //创建一个空的文件到沙河中
- NSFileManager * mgr = [NSFileManager defaultManager];
- [mgr createFileAtPath:filepath contents:nil attributes:nil];
- //创建一个用来写数据的文件句柄
- self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
- //获取文件的大小
- self.totalLength = response.expectedContentLength;
- NSLog(@"--%lld",self.totalLength);
- // 68186536
- }
- /**
- * 2.当接收到服务器返回的实体数据时调用(具体内容,这个方法可能会被调用多次)
- *
- * @param data 这次返回的数据
- */
- -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
- //移动到文件的最后面
- [self.writeHandle seekToEndOfFile];
- //将数据写入到沙河
- [self.writeHandle writeData:data];
- // NSLog(@"%@",data);
- //累计文件的长度
- self.currentLength += data.length;
- self.speed = [CountBytes CountBytesBy:data.length];
- self.CircleView.progress = (double)self.currentLength/ self.totalLength;
- }
文件下载二(支持断点续传)
所谓断点续传就是在某个时刻暂停后,再次下载的时候从之前的进度开始,这就需要我们记录下暂停之前下载数据。
ok,我们只看暂停和再次开始的代码
暂停的代码
- - (IBAction)pauseClicked:(id)sender {
- [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
- _resumeData = resumeData;
- _task = nil;
- }];
- }
再次恢复下载的代码
- - (IBAction)continueClicked:(id)sender{
- self.task = [self.session downloadTaskWithResumeData:self.resumeData];
- [self.task resume];
- self.resumeData = nil;
- }
文件下载三(支持断点续传)
这种下载方式是站在巨人的肩膀上,这个巨人就是伟大的AFN,在这里我将其封装成了下载工具类,只需要传下载路径就ok,然后根据block返回的数据进行刷新线程。
核心代码
- + (NSURLSessionDownloadTask *)downloadFileWithUrl:(NSString *)url DownloadProgress:(DownloadProgress)progress DownloadCompletion:(CompletionState)completion{
- // 1、 设置请求
- NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
- // 2、初始化
- NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
- // 3、开始下载
- NSURLSessionDownloadTask * task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
- progress(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount,1.0 * downloadProgress.totalUnitCount,1.0 * downloadProgress.completedUnitCount);
- } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
- //这里要返回一个NSURL,其实就是文件的位置路径
- NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
- //使用建议的路径
- path = [path stringByAppendingPathComponent:response.suggestedFilename];
- NSLog(@"%@",path);
- return [NSURL fileURLWithPath:path];//转化为文件路径
- } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
- //如果下载的是压缩包的话,可以在这里进行解压
- NSLog(@"----%@---%d---%@",error.domain,error.code,error);
- //下载成功
- if (error == nil) {
- completion(YES,@"下载完成",[filePath path]);
- }else{//下载失败的时候,只列举判断了两种错误状态码
- NSString * message = nil;
- if (error.code == - 1005) {
- message = @"网络异常";
- }else if (error.code == -1001){
- message = @"请求超时";
- }else{
- message = @"未知错误";
- }
- completion(NO,message,nil);
- }
- }];
- return task;
- }
代码传送门: https://github.com/fuzongjian/DownloadStudy.git,