VVFileTransfer
功能
- 暂停,取消下载后,再次下载,可从上次进度继续
- App切至后台,可以继续下载
- App正常/非正常退出后,再次打开App可以继续上次进度
- 可变化下载地址续传(google cloud)
- 多文件同时下载
- 断点上传,切片上传(需服务器支持),未测试
- 文件切片处理工具
注意
如果在Capabilities中开启了Background Fetch,杀死App后还会继续下载,但再次开启App回重新下载.因为下载已经完成,或者恢复下载的数据已经发生了变化.
安装
VVFileTransfer 支持 CocoaPods. 请在Podfile中加入:
pod 'VVFileTransfer'
使用
下载:
NSURLSessionDownloadTask *task = [[VVFileTransfer defaultTransfer] downloadTaskWithURL:url progress:^(NSProgress *downloadProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
xxx.progressView.progress = downloadProgress.fractionCompleted;
});
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
//这里要返回一个NSURL,其实就是文件的位置路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//使用建议的路径
path = [path stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"%@",path);
return [NSURL fileURLWithPath:path];//转化为文件路径
} completionHandler:^(NSURLResponse *response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
NSLog(@"下载成功: %@", filePath);
}else{
NSLog(@"error: %@", error);
}
}]
文件处理:
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingPathComponent:@"test.jpg"];
VVFileHelper *helper = [[VVFileHelper alloc] initWithFileAtPath:path];
helper.useStream = YES;
[helper handleItem:^NSData *(NSData *sliceData) {
// 此处可对文件进行分片加密等
return sliceData;
} sliceSize:32 * 1024 progress:^(NSProgress *progress) {
self.progressView.progress = progress.fractionCompleted;
NSLog(@"progress: %@", @(progress.fractionCompleted));
} completion:^(NSString * _Nullable dstPath, NSError * _Nullable error) {
NSLog(@"dstPath: %@\nerror: %@", dstPath, error);
}];
Author
pozi119, [email protected]
License
VVFileTransfer is available under the MIT license. See the LICENSE file for more info.