美文网首页
AFN3.0前后的断点下载

AFN3.0前后的断点下载

作者: 十一岁的加重 | 来源:发表于2017-08-23 08:52 被阅读125次

3.0前用AFHTTPRequestOperation

#pragma mark 下载文件
/**
 下载文件
 
 @param willDownloadFilePath 文件地址
 @param isSupportBreakpointResume 是否支持断点续传
 @param downloadProgressBlock 下载进度
 @param downloadSuccessBlock 成功块
 @param downloadFailureBlock 失败块
 */
- (void)downloadFileAtPath:(NSString *)willDownloadFilePath supportBreakpointResume:(BOOL)isSupportBreakpointResume downloadProgressBlock:(void (^)(float downloadProgress))downloadProgressBlock downloadSuccessBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))downloadSuccessBlock downloadFailureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))downloadFailureBlock {
    
    if (!(willDownloadFilePath && willDownloadFilePath.length)) {
        
        NSLog(@"--下载地址--%@--有问题--停止下载--", willDownloadFilePath);
        return;
    }
    NSString *localCacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *localDownloadFilePath = [localCacheDir stringByAppendingPathComponent:[willDownloadFilePath lastPathComponent]];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:willDownloadFilePath]];
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    if (!isSupportBreakpointResume) {
        [fileMgr removeItemAtPath:localDownloadFilePath error:nil];
        
        AFHTTPRequestOperation *downloadOp = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        downloadOp.outputStream = [NSOutputStream outputStreamToFileAtPath:localDownloadFilePath append:YES];
        [downloadOp setCompletionBlockWithSuccess:downloadSuccessBlock failure:downloadFailureBlock];
        [downloadOp start];
        
        return;
    }
    
    ///  检查文件是否已经下载了一部分
    unsigned long long downloadedBytes = 0;
    if ([fileMgr fileExistsAtPath:localDownloadFilePath]) {
        //获取已下载的文件长度
        downloadedBytes = [self fileSizeForPath:localDownloadFilePath];
        if (downloadedBytes > 0) {
            NSMutableURLRequest *mutableURLRequest = [request mutableCopy];
            NSString *requestRange = [NSString stringWithFormat:@"bytes=%llu-", downloadedBytes];
            [mutableURLRequest setValue:requestRange forHTTPHeaderField:@"Range"];
            request = mutableURLRequest;
        } else {
            [fileMgr removeItemAtPath:localDownloadFilePath error:nil];
        }
    }
    //不使用缓存,避免断点续传出现问题
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
    
    AFHTTPRequestOperation *downloadOp = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    downloadOp.outputStream = [NSOutputStream outputStreamToFileAtPath:localDownloadFilePath append:YES];
    if (downloadProgressBlock) {
        
        [downloadOp setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            
            float downloadProgress = ((float)totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes);
            downloadProgressBlock(downloadProgress);
        }];
    }
    [downloadOp setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSLog(@"--文件--下载成功--%@--", willDownloadFilePath);
        if (downloadSuccessBlock) {
            downloadSuccessBlock(operation, responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
        NSLog(@"--文件--下载失败--%@--", error.localizedDescription);
        if (downloadFailureBlock) {
            downloadFailureBlock(operation, error);
        }
    }];
    
    // 开启下载
    [downloadOp start];
}


3.0后就没这么简单了,得多写点代码
可参考:
iOS + AFN3.0 断点下载及异常中断处理

相关文章

  • AFN3.0前后的断点下载

    3.0前用AFHTTPRequestOperation 3.0后就没这么简单了,得多写点代码可参考:iOS + A...

  • iOS + AFN3.0 断点下载及异常中断处理

    断点下载是很常见的一个需求,AFN3.0 也为我们提供了下载的方法,但要实现断点下载,还需要我们自己另行处理。不过...

  • AFN 3.0 小结

    GET请求 POST请求 AFN3.0下载方法 AFN3.0 上传 监测当前网络状态(网络监听) 勤学如早春之苗,...

  • 基于Okhttp实现断点下载(续传)和分片下载

    断点下载/续传 断点下载是针对下载大文件需求的一种优化机制,可以从上次下载的断点处继续下载。断点续传原理也相同,只...

  • AFN3.0前后的区别

    一、发展历程 AFNetworking 1.0建立在NSURLConnection的基础API之上。 AFNetw...

  • OC网络:NSURLSession-downloadTask的断

    NSURLSessionDownloadDelegate断点下载说明: 能实现断点下载,即在程序不退出的情况下:暂...

  • 断点下载

    效果图 断点下载的过程 这里是简单的一个下载,下一篇介绍列表的断点下载1.绘制UI图,并初始化控件2.创建实体类:...

  • 断点下载

    今天做了断点下载视频或者MP3,具体实现如下 1、主要应用了NSURLSessionDownloadTask和NS...

  • 断点下载

    链接转载;判断当前Service是否是重启的Servciehttp://blog.csdn.net/luyi325...

  • 断点下载

    http://www.jianshu.com/p/f65e32012f07

网友评论

      本文标题:AFN3.0前后的断点下载

      本文链接:https://www.haomeiwen.com/subject/gmzrdxtx.html