iOS 多个文件下载和管理 (二)

在前面第一章 点击打开链接 的时候,介绍了NSURLSessionDownloadTask

这一章节就讲,在一章节的基础上,进一步封装,能管理每个任务

BackgroundDownloadTool  相关代码,看第一章节:点击打开链接 ,这里就不说了

使用:主要演示了,两个任务

#import "ViewController.h"
#import "DownloadManagement.h"

@interface ViewController () {
    DownloadManagement *DM;
}

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UILabel *label2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    DM = [DownloadManagement sharedInstance];
    
    [DM downloadFromURL:@"http://baobab.wdjcdn.com/1455888619273255747085_x264.mp4" fileName:@"test.mp4" identifier: @"one" Progress:^(CGFloat progress, NSError *err) {
        self.label.text = [NSString stringWithFormat:@"%f",progress];

    } complement:^(NSString *path) {
        self.label.text = @"100%";
        
    }];
    
    [DM downloadFromURL:@"http://baobab.wdjcdn.com/1455888619273255747085_x264.mp4" fileName:@"test2.mp4" identifier: @"two" Progress:^(CGFloat progress, NSError *err) {
        self.label2.text = [NSString stringWithFormat:@"%f",progress];

    } complement:^(NSString *path) {
        self.label2.text = @"100%";
        
    }];
    
}

- (IBAction)start:(id)sender {
    [DM startOrContinueDownload:@"one"];
}

- (IBAction)stop:(id)sender {
    [DM pauseDownload:@"one"];
}

- (IBAction)cancel:(id)sender {
    [DM cancelDownload:@"one"];
}

- (IBAction)start2:(id)sender {
    [DM startOrContinueDownload:@"two"];
}

- (IBAction)stop2:(id)sender {
    [DM pauseDownload:@"two"];
}

- (IBAction)cancel2:(id)sender {
    [DM cancelDownload:@"two"];
}

- (IBAction)allStart:(id)sender {
    [DM startOrContinueAllTasks];
}

- (IBAction)allStop:(id)sender {
    [DM pauseAllTasks];
}

- (IBAction)allCancel:(id)sender {
    [DM cancelAllTasks];
}

- (IBAction)delete:(id)sender {
    [DM removeDownloadFile:@"one"];
}

- (IBAction)delete2:(id)sender {
    [DM removeDownloadFile:@"two"];
}

- (IBAction)delteAll:(id)sender {
    [DM removeAllDownloadFile];
}

- (IBAction)deleteCache1:(id)sender {
    [DM removeCacheDownloadFile:@"one"];
}

- (IBAction)deleteCache2:(id)sender {
    [DM removeCacheDownloadFile:@"two"];
}

- (IBAction)deleteAllCache:(id)sender {
    [DM removeAllCacheDownloadFile];
}


主要代码:

DownloadManagement.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface DownloadManagement : NSObject
@property (nonatomic, copy) void (^backgroundSessionCompletionHandler)();

+(DownloadManagement *)sharedInstance;

-(void)startOrContinueDownload:(NSString *)identifier;//开始或继续某个任务
- (void)startOrContinueAllTasks;//开始或继续所有任务

-(void)pauseDownload:(NSString *)identifier;//暂停某个任务
- (void)pauseAllTasks;//暂停所有任务

-(void)cancelDownload:(NSString *)identifier;//取消某个任务
- (void)cancelAllTasks;//取消所有任务

-(void)removeDownloadFile:(NSString *)identifier;//移除已下载好的某个文件
-(void)removeAllDownloadFile;//移除已下载好的所有文件

-(void)removeCacheDownloadFile:(NSString *)identifier;//移除缓存的某个文件
-(void)removeAllCacheDownloadFile;//移除缓存的所有文件

- (void)downloadFromURL:(NSString *)urlStr fileName:(NSString *)fileName identifier:(NSString *)identifier Progress:(void (^)(CGFloat progress, NSError *err))downloadProgressBlock complement:(void (^)(NSString *path))completeBlock;

@end


DownloadManagement.m

#import "DownloadManagement.h"
#import "BackgroundDownloadTool.h"

@interface DownloadManagement ()
@property(nonatomic,strong)NSMutableDictionary *taskDic;
@end

@implementation DownloadManagement
-(NSMutableDictionary *)taskDic {
    if (!_taskDic) {
        _taskDic = [NSMutableDictionary dictionary];
    }
    return _taskDic;
}

-(instancetype)init{
    if (self = [super init]) {
       
    }
    return self;
}

+(DownloadManagement *)sharedInstance {
    static dispatch_once_t pred = 0;
    __strong static id internet = nil;
    dispatch_once(&pred, ^{
        internet = [[self alloc] init];
    });
    return internet;
}

- (void)downloadFromURL:(NSString *)urlStr fileName:(NSString *)fileName identifier:(NSString *)identifier Progress:(void (^)(CGFloat progress, NSError *err))downloadProgressBlock complement:(void (^)(NSString *path))completeBlock {
    if (![self.taskDic.allKeys containsObject:identifier]) {
        BackgroundDownloadTool *tool = [BackgroundDownloadTool new];
        tool.backgroundSessionCompletionHandler = self.backgroundSessionCompletionHandler;
        tool.urlStr = urlStr;
        tool.fileName = fileName;
        tool.identifier = identifier;
        [tool downloadProgress:^(CGFloat progress, NSError *err) {
            downloadProgressBlock(progress,err);
        } complement:^(NSString *path) {
            //[self.taskDic removeObjectForKey:identifier];
            completeBlock(path);
        }];
        [self.taskDic setObject:tool forKey:identifier];
    }
}

-(void)startOrContinueDownload:(NSString *)identifier {
    BackgroundDownloadTool *tool = self.taskDic[identifier];
    if (tool) {
        [tool startOrContinueDownload];
    }
}

- (void)startOrContinueAllTasks {
    [self.taskDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, BackgroundDownloadTool  *_Nonnull obj, BOOL * _Nonnull stop) {
        [obj startOrContinueDownload];
    }];
}

-(void)pauseDownload:(NSString *)identifier {
    BackgroundDownloadTool *tool = self.taskDic[identifier];
    if (tool) {
        [tool pauseDownload];
    }
}

- (void)pauseAllTasks {
    [self.taskDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, BackgroundDownloadTool  *_Nonnull obj, BOOL * _Nonnull stop) {
        [obj pauseDownload];
    }];
}

-(void)cancelDownload:(NSString *)identifier {
    BackgroundDownloadTool *tool = self.taskDic[identifier];
    if (tool) {
        [tool cancelDownload];
    }
}

- (void)cancelAllTasks {
    [self.taskDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, BackgroundDownloadTool  *_Nonnull obj, BOOL * _Nonnull stop) {
        [obj cancelDownload];
    }];
}

-(void)removeDownloadFile:(NSString *)identifier {
    BackgroundDownloadTool *tool = self.taskDic[identifier];
    if (tool) {
        [tool removeDownloadFile];
    }
}

-(void)removeAllDownloadFile {
    [self.taskDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, BackgroundDownloadTool  *_Nonnull obj, BOOL * _Nonnull stop) {
        [obj removeDownloadFile];
    }];
}

-(void)removeCacheDownloadFile:(NSString *)identifier {
    BackgroundDownloadTool *tool = self.taskDic[identifier];
    if (tool) {
        [tool removeCacheDownloadFile];
    }
}

-(void)removeAllCacheDownloadFile {
    [self.taskDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, BackgroundDownloadTool  *_Nonnull obj, BOOL * _Nonnull stop) {
        [obj removeCacheDownloadFile];
    }];
}

@end

我的业余技术微信公众号:YKJGZH,欢迎大家进入


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值