iOS断点续传

一、原生代码
NSURLSession里面用三个任务 NSURLSessionDataTask、NSURLSessionDownloadTask、NSURLSessionUploadTask
.h

#import <Foundation/Foundation.h>

@protocol FileDownLoadDelegate <NSObject>
@optional
- (void)backDownprogress:(float)progress tag:(NSInteger)tag;
- (void)downSucceed:(NSURL*)url tag:(NSInteger)tag;
- (void)downError:(NSError*)error tag:(NSInteger)tag;
@end

@interface FileDownloadNetWorkNative : NSObject

@property (nonatomic, strong) NSURLSession* session;
@property (nonatomic, strong) NSURLSessionDownloadTask* downloadTask;
@property (nonatomic, strong) NSData* resumeData;
@property (nonatomic, weak) id<FileDownLoadDelegate> myDeleate;
@property (nonatomic, assign) NSInteger tag;//某个文件下载的的标记
///单例
+(instancetype)shareManagerDownLoad;
///fileUrl:下载地址
-(void)downFile:(NSString*)fileUrl;
///暂停或者继续下载
-(void)suspendDownload;
///取消下载
-(void)cancelDownload;

@end

.m

#import "FileDownloadNetWorkNative.h"
#import <CommonCrypto/CommonDigest.h>

@interface FileDownloadNetWorkNative ()<NSURLSessionDelegate>
@property (nonatomic) BOOL  mIsSuspend;

@end

@implementation FileDownloadNetWorkNative

//闪退或者强制退出 初始化该方法会走didCompleteWithError代理方法
+(instancetype)shareManagerDownLoad{
    static FileDownloadNetWorkNative *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[self alloc] init];
    });
    return shareManager;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.lingxin.app2"];
//         允许蜂窝网络: 你可以做偏好设置
        config.allowsCellularAccess = YES;
        config.timeoutIntervalForRequest = 30;
//        创建一个下载线程
        self.session = [NSURLSession sessionWithConfiguration:config
                                                     delegate:self
                                                delegateQueue:[NSOperationQueue mainQueue]];
    }
    return self;
}

-(void)downFile:(NSString *)fileUrl{
    if (!fileUrl || fileUrl.length == 0 || ![self checkIsUrlAtString:fileUrl]) {
        NSLog(@"fileUrl 无效");
        return ;
    }
    NSURL *url = [NSURL URLWithString:fileUrl];
    NSURLSessionDownloadTask   *downloadTask = nil;
    NSData *resumeData = [self getResumeData:fileUrl];
    if (resumeData.length>0) {//断点续传
        downloadTask = [self.session downloadTaskWithResumeData:resumeData];
    }else{//重新开始下载
        downloadTask = [self.session downloadTaskWithURL:url];
    }
    self.downloadTask = downloadTask;
    [downloadTask resume];
}

#pragma mark - NSURLSessionDelegate
/* 下载过程中调用,用于跟踪下载进度
 * bytesWritten为单次下载大小
 * totalBytesWritten为当当前一共下载大小
 * totalBytesExpectedToWrite为文件大小
 */
//每次传一个包 调用一次该函数 512M
-(void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    float dowProgeress = 1.0 * totalBytesWritten / totalBytesExpectedToWrite;
//        NSLog(@"🍎🍎🍎进度:%f",dowProgeress);
    if (self.myDeleate && [self.myDeleate respondsToSelector:@selector(backDownprogress:tag:)]) {
        [self.myDeleate backDownprogress:dowProgeress tag:self.tag];
    }
}

/*
 2.下载完成之后调用该方法
 */
-(void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location{
    NSString *url = downloadTask.currentRequest.URL.absoluteString;
//    文件储存路径
    NSString *storagePath = [self downLoadSuccessDataDiskTmpPath:url];
    //创建文件管理器
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath: storagePath]) {
        //如果文件夹下有同名文件  则将其删除
        [manager removeItemAtPath:storagePath error:nil];
    }
    NSError *saveError;
//    把缓存文件移动到指定的沙盒路径
    [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:storagePath] error:&saveError];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            NSURL *url = [[NSURL alloc]initFileURLWithPath:storagePath];
            if(self.myDeleate && [self.myDeleate respondsToSelector:@selector(downSucceed:tag:)])
                [self.myDeleate downSucceed:url tag:self.tag];
        });
    NSString *resumeDataPath = [self resumeDataDiskTmpPath:url];
    if ([manager fileExistsAtPath:resumeDataPath]) {//删除磁盘中的缓存数据
        [manager removeItemAtPath:resumeDataPath error:nil];
    }
    if ([manager fileExistsAtPath:location.path]) {
        [manager removeItemAtPath:location.path error:nil];
    }

}
/* 在任务下载完成、下载失败
 * 或者是应用被杀掉后,重新启动应用并创建相关identifier的Session时调用
 */
//下载失败和完成都会调用,cancel时错误为-999
-(void)URLSession:(nonnull NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error{
    NSString *url = task.currentRequest.URL.absoluteString;
    NSString *resumeDataPath = [self resumeDataDiskTmpPath:url];
    if (error) {
        
        if(error && self.myDeleate && [self.myDeleate respondsToSelector:@selector(downError:tag:)] && error.code != -999){//回调非取消时的错误
            [self.myDeleate downError:error tag:self.tag];
        }
        NSData *resumeData = [error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData];
        [resumeData writeToFile:resumeDataPath atomically:NO];
        
    }else{//成功时调用
        if (self.myDeleate && [self.myDeleate respondsToSelector:@selector(backDownprogress:tag:)]) {
            [self.myDeleate backDownprogress:1 tag:self.tag];//解决后台情况下下载完成后进度条没有更新的问题
        }
    }
}
/* 应用在后台,而且后台所有下载任务完成后,
 * 在所有其他NSURLSession和NSURLSessionDownloadTask委托方法执行完后回调,
 * 可以在该方法中做下载数据管理和UI刷新
 *最好将handleEventsForBackgroundURLSession中completionHandler保存,在该方法中待所有载数据管理和UI刷新做完后,再调用completionHandler()
 */
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
// 调用在 -application:handleEventsForBackgroundURLSession: 中保存的 handler
    NSLog(@"所有后台任务已经完成: %@",session.configuration.identifier);
    
}
/* 下载恢复时调用
 * 在使用downloadTaskWithResumeData:方法获取到对应NSURLSessionDownloadTask,
 * 并该task调用resume的时候调用
 */
- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
 didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes{
    
}



#pragma mark - private
//暂停下载
-(void)suspendDownload{
    
    if (self.mIsSuspend) {
        [self.downloadTask resume];
    }else{
        [self.downloadTask suspend];
    }
    self.mIsSuspend = !self.mIsSuspend;
}

//取消下载
-(void)cancelDownload{
    
    __weak typeof(self) weakSelf = self;
    [self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
//        weakSelf.downloadTask  = nil;
//        [resumeData writeToFile:[self resumeDataDiskTmpPath:url] atomically:NO];
    }];
    
    
}

//获取resumedata数据
-(NSData *)getResumeData:(NSString *)url{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSData *datas     = [fm contentsAtPath:[self resumeDataDiskTmpPath:url]];
    return datas;
}
//resumeData数据临时路径,在library中
-(NSString *)resumeDataDiskTmpPath:(NSString *)url{
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    NSString *tmpPath = [NSString stringWithFormat:@"%@/resumeDataTmpFile",libraryPath];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    //    判断storePath路径下文件是否存在,以及storePath路径是否是存在的目录
    BOOL exist = [manager fileExistsAtPath:tmpPath isDirectory:&isDir];
    if (!(isDir == YES && exist == YES)) {
        [manager createDirectoryAtPath:tmpPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath = [tmpPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.tmp",[self md5:url]]];//resumeDataTmpFile/
    return filePath;
}
//下载成功的数据存储路径,在document中
-(NSString *)downLoadSuccessDataDiskTmpPath:(NSString *)url{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
    NSString *storePath = [NSString stringWithFormat:@"%@/downLoadSuccessFile",documentPath];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    //    判断storePath路径下文件是否存在,以及storePath路径是否是存在的目录
    BOOL exist = [manager fileExistsAtPath:storePath isDirectory:&isDir];
    if (!(isDir == YES && exist == YES)) {
        [manager createDirectoryAtPath:storePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath = [storePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",[self md5:url]]];//downLoadSuccessFile/
    return filePath;
}

//用url获取文件名称 (MD5加密)
- (NSString *)md5:(NSString *)string{
    const char *cStr = [string UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02X", digest[i]];
    }
    return result;
}

- (BOOL)checkIsUrlAtString:(NSString *)url {
    NSString *pattern = @"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSArray *regexArray = [regex matchesInString:url options:0 range:NSMakeRange(0, url.length)];
    
    if (regexArray.count > 0) {
        return YES;
    }else {
        return NO;
    }
}

- (void)dealloc
{
    [self.session invalidateAndCancel];
    self.session = nil;
    [self.downloadTask cancel];
    self.downloadTask = nil;
}

/**
 AppDelegate 中要实现- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler//在应用处于后台,且后台下载的所有任务完成后才会调用
 在后台情况下才会执行-(void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location方法,-(void)URLSession:(nonnull NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error方法,- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session方法
 */

@end

二、AFNetworking
.h

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

typedef void (^FileDownLoadSuccessBlock)(NSURL *fileUrlPath ,NSURLResponse *  response );
typedef void (^FileDownLoadFailBlock)(NSError*  error ,NSInteger statusCode);
typedef void (^FileDownLoadProgress)(CGFloat  progress);

@interface FileDownLoadNetwork : NSObject
///单例
+(instancetype)shareManagerDownLoad;
//下载文件
-(NSURLSessionDownloadTask *)downloadFileWithFileUrl:(NSString *)requestUrl progress:(FileDownLoadProgress)progressBlock success:(FileDownLoadSuccessBlock)successBlock failure:(FileDownLoadFailBlock)failBlock;
///根据url取消下载
-(void)cancelDownloadTaskWithUrl:(NSString *)url;
///根据task取消下载
-(void)cancelDownloadTask:(NSURLSessionDownloadTask *)task;
///取消所有的下载任务
- (void)cancelAllCurrentDownLoadTasks;

@end

.m

#import "FileDownLoadNetwork.h"
#import <CommonCrypto/CommonDigest.h>

@interface FileDownLoadNetwork()
/**  */
@property (nonatomic,strong) AFURLSessionManager *manager;
/** 为了解决后台情况下下载完成后,进度条不能及时更新的问题 ,如果AF的版本是3.0.0-3.1.0则不用使用该字典,这些版本在后台下载完成后,progress的block能够回调,3.2.0以上的版本在后台下载完成后,progress的block不回调,不能及时更新进度条,所以要使用该字典解决*/
@property (nonatomic,strong) NSMutableDictionary *blockDic;
/**  */
//@property (nonatomic,assign) BOOL progressBlockTag;


@end

@implementation FileDownLoadNetwork
- (NSMutableDictionary *)blockDic{
    if (!_blockDic) {
        _blockDic = [[NSMutableDictionary alloc]init];
    }
    return _blockDic;
}
+(instancetype)shareManagerDownLoad{
    
    static FileDownLoadNetwork *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[self alloc] init];
    });
    return shareManager;
}
/**
 后台下载
 下载完成之后杀死app,再创建任务时taskIdentifier为1
 未下载完成就杀死app,再创建任务时taskIdentifier在上个taskIdentifier的基础上增加,比如杀死前taskIdentifier的最大值为3,那么创建时taskIdentifier为4
 默认下载
 只要杀死app,再创建任务时taskIdentifier为1
 */
- (instancetype)init{
    self = [super init];
    if (self) {
//        配置(可以后台下载)
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.lingxin.app"];
        configuration.timeoutIntervalForRequest = 30;
//        是否允许蜂窝网络
        configuration.allowsCellularAccess = YES;
        self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
        NSLog(@"👩‍🍳👩‍🍳👩‍🍳👩‍🍳👩‍🍳👩‍🍳初始化单例");
        NSURLSessionDownloadTask *task;
//        下载完成,取消,下载失败的通知
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(downloadData:)
                                                     name:AFNetworkingTaskDidCompleteNotification
                                                   object:task];
    }
    return  self;
}

- (NSURLSessionDownloadTask *)downloadFileWithFileUrl:(NSString *)requestUrl progress:(FileDownLoadProgress)progressBlock success:(FileDownLoadSuccessBlock)successBlock failure:(FileDownLoadFailBlock)failBlock{
    
    [self.blockDic setObject:progressBlock forKey:requestUrl];
    NSURLSessionDownloadTask   *downloadTask = nil;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestUrl]];
    NSData *resumeData = [self getResumeData:requestUrl];
    NSLog(@"本地存储的需要续传的数据长度为: %ld",resumeData.length);
    if (resumeData.length>0) {//断点续传
        NSLog(@"断点续传下载");
        downloadTask = [self.manager downloadTaskWithResumeData:resumeData progress:^(NSProgress * _Nonnull downloadProgress) {
            if (progressBlock) {
                progressBlock(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
                NSLog(@"jhl断点续传任务进度:%F",(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount));
            }
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//        targetPath:缓存路径,在沙盒里的library/cache中,下载成功后targetPath下的缓存数据会被删除,下载的文件进入到了返回的存储路径下
            return [NSURL fileURLWithPath:[self downLoadSuccessDataDiskTmpPath:requestUrl]];//返回文件存储路径
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//            filePath:文件存储路径
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
            if ([httpResponse statusCode] == 404) {
                [[NSFileManager defaultManager] removeItemAtURL:filePath error:nil];
            }
            if (error) {////取消也会报错 statusCode为206 error的code为-999
                if (failBlock) {
                    failBlock(error,[httpResponse statusCode]);
                }
            }else{
                if (successBlock) {
                    successBlock(filePath,response);
                }
            }
        }];
        
    }else{//从头开始下载
        NSLog(@"重新开始下载");
        downloadTask = [self.manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
            if (progressBlock) {
                progressBlock(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
            }
            NSLog(@"jhl新任务进度:%F",(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount));
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            return [NSURL fileURLWithPath:[self downLoadSuccessDataDiskTmpPath:requestUrl]];

        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
            if ([httpResponse statusCode] == 404) {
                [[NSFileManager defaultManager] removeItemAtURL:filePath error:nil];
            }
            if (error) {
                if (failBlock) {//取消也会报错 statusCode为206 error的code为-999
                    failBlock(error,[httpResponse statusCode]);
                }
            }else{
                if (successBlock) {
                    successBlock(filePath,response);
                }
            }
        }];
    }
    [downloadTask resume];
    return downloadTask;
}
/**
 什么时候收到通知
 1.下载成功时,此时error为nil
 2.下载失败时,如果是取消了,错误code为-999,如果是网络原因,错误code为-1001
 3.任务还未下载完成,app强制退出或者闪退后,再次进入app初始化season时,此时收到通知,保存app强制退出或者闪退时系统帮忙存储的resumedata,以便断点续传时使用。
 app杀死后系统帮忙存储resumeData条件:
 配置必须使用backgroundSessionConfigurationWithIdentifier:方法
 */
-(void)downloadData:(NSNotification *)notify{
    if ([notify.object isKindOfClass:[ NSURLSessionDownloadTask class]]) {
        NSURLSessionDownloadTask *task = notify.object;
        NSString *url = [task.currentRequest.URL absoluteString];
        NSError *error  = [notify.userInfo objectForKey:AFNetworkingTaskDidCompleteErrorKey] ;
        NSString *resumeDataPath = [self resumeDataDiskTmpPath:url];
        NSLog(@"通知里的🍎🍎🍎:%@",error);
        
        if (error) {
//        code为-1是The request timed out
            if (error.code == -1001) {//网络原因
                
            }else if (error.code == -999){//取消时的错误
                
                NSData *resumeData = [error.userInfo objectForKey:@"NSURLSessionDownloadTaskResumeData"];
                //            存储强制退出或者闪退后系统帮忙存储的resumedata数据
                [resumeData writeToFile:resumeDataPath atomically:NO];
            }

        }else{//下载成功
            FileDownLoadProgress progressBlock = [self.blockDic objectForKey:url];
            if (progressBlock) {
                NSLog(@"🍊🍊🍊🍊🍊:%@",[self.blockDic allValues]);
                progressBlock(1.0);//更新进度
                [self.blockDic removeObjectForKey:url];
            }
            NSFileManager *manager = [NSFileManager defaultManager];
            if ([manager fileExistsAtPath:resumeDataPath]) {
//                移除缓存文件,只有app取消,闪退,强制退出时resumeDataPath路径下的文件才会存在
                [manager removeItemAtPath:resumeDataPath error:nil];
                NSLog(@"缓冲的resumeData文件已经被移除");
            }
        }
    }
}
//获取resumedata数据
-(NSData *)getResumeData:(NSString *)url{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSData *datas     = [fm contentsAtPath:[self resumeDataDiskTmpPath:url]];
    return datas;
}
//resumeData数据临时路径,在library中
-(NSString *)resumeDataDiskTmpPath:(NSString *)url{
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    NSString *tmpPath = [NSString stringWithFormat:@"%@/resumeDataTmpFile",libraryPath];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL isDir = NO;
//    判断storePath路径下文件是否存在,以及storePath路径是否是存在的目录
    BOOL exist = [manager fileExistsAtPath:tmpPath isDirectory:&isDir];
    if (!(isDir == YES && exist == YES)) {
        [manager createDirectoryAtPath:tmpPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath = [tmpPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.tmp",[self md5:url]]];//resumeDataTmpFile/
    return filePath;
}
//下载成功的数据存储路径,在document中
-(NSString *)downLoadSuccessDataDiskTmpPath:(NSString *)url{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
    NSString *storePath = [NSString stringWithFormat:@"%@/downLoadSuccessFile",documentPath];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL isDir = NO;
//    判断storePath路径下文件是否存在,以及storePath路径是否是存在的目录
   BOOL exist = [manager fileExistsAtPath:storePath isDirectory:&isDir];
    if (!(isDir == YES && exist == YES)) {
        [manager createDirectoryAtPath:storePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath = [storePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",[self md5:url]]];//downLoadSuccessFile/
    return filePath;
}

//用url获取文件名称 (MD5加密)
- (NSString *)md5:(NSString *)string{
    const char *cStr = [string UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02X", digest[i]];
    }
    return result;
}
//根据url取消下载
-(void)cancelDownloadTaskWithUrl:(NSString *)url{
    for (NSURLSessionDownloadTask *task in self.manager.downloadTasks) {
        if ([task.currentRequest.URL.absoluteString isEqualToString:url]) {
            if (task.state == NSURLSessionTaskStateRunning) {
                
                [task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
                    
                }];
            }
        }
    }
}
//根据task取消下载
-(void)cancelDownloadTask:(NSURLSessionDownloadTask *)task{
    if (task.state == NSURLSessionTaskStateRunning) {
        [task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
            //       这里也可以存储resumeData,通知方法downloadData:中也可以存储
        }];
    }
}
//停止当前所有的下载任务
- (void)cancelAllCurrentDownLoadTasks{
    if ([[self.manager downloadTasks] count]  == 0) {
        return;
    }
    for (NSURLSessionDownloadTask *task in  [self.manager downloadTasks]) {
        if (task.state == NSURLSessionTaskStateRunning) {
            [task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
                
            }];
        }
    }
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
//获取当前时间 下载id标识用
- (NSString *)currentDateStr{
    NSDate *currentDate = [NSDate date];//获取当前时间,日期
    NSTimeInterval timeInterval = [currentDate timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.f",timeInterval];
}
/**
 AppDelegate 中要实现- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler//在应用处于后台,且后台下载的所有任务完成后才会调用
 app才能在后台情况下,执行通知和block代码块,不实现的话,当app进入前台时才能执行通知和block代码块
 */

@end

demo:https://github.com/jiahonglingkaixinmeiyitian/download.git

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,302评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,232评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,337评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,977评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,920评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,194评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,638评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,319评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,455评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,379评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,426评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,106评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,696评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,786评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,996评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,467评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,043评论 2 341

推荐阅读更多精彩内容