iOS 多个文件下载和管理 (二)
来源:互联网 发布:linux samba rpm 编辑:程序博客网 时间:2024/09/21 06:32
在前面第一章 点击打开链接 的时候,介绍了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
阅读全文
0 0
- iOS 多个文件下载和管理 (二)
- iOS文件下载(二)
- iOS文件管理(二)
- IOS开发-文件管理(二)
- IOS开发-文件管理(二)
- FileUpload框架实现文件上传(多个文件)和下载
- 文件下载(二)
- IOS文件的上传与下载(二)
- IOS管理文件和目录
- IOS管理文件和目录
- ios文件和文件夹管理
- IOS管理文件和目录
- IOS管理文件和目录
- IOS管理文件和目录
- iOS 管理文件和目录
- iOS管理文件和目录
- IOS管理文件和目录
- iOS网络--『文件下载、断点下载』的实现(二):NSURLSession
- Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-plugin-plugi
- leetcode 304.Range Sum Query 2D
- final修饰符
- 移植qt5.6.0到armv7 <完整过程>
- 虚拟篇 04. 检查点的作用 ❀ Windows Server 2016
- iOS 多个文件下载和管理 (二)
- PowerDesigner使用教程
- OI知识结构图
- UIAlertConntroller
- js函数重载方法2 待 ----未看懂
- AR的引用
- [绍棠] FMDB的方法介绍
- 给初学者的RxJava2.0教程(五)
- 十大经典排序算法的JS版