iOS斷點(diǎn)續(xù)傳

一、原生代碼
NSURLSession里面用三個(gè)任務(wù) 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;//某個(gè)文件下載的的標(biāo)記
///單例
+(instancetype)shareManagerDownLoad;
///fileUrl:下載地址
-(void)downFile:(NSString*)fileUrl;
///暫?;蛘呃^續(xù)下載
-(void)suspendDownload;
///取消下載
-(void)cancelDownload;

@end

.m

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

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

@end

@implementation FileDownloadNetWorkNative

//閃退或者強(qiáng)制退出 初始化該方法會(huì)走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"];
//         允許蜂窩網(wǎng)絡(luò): 你可以做偏好設(shè)置
        config.allowsCellularAccess = YES;
        config.timeoutIntervalForRequest = 30;
//        創(chuàng)建一個(gè)下載線程
        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) {//斷點(diǎn)續(xù)傳
        downloadTask = [self.session downloadTaskWithResumeData:resumeData];
    }else{//重新開始下載
        downloadTask = [self.session downloadTaskWithURL:url];
    }
    self.downloadTask = downloadTask;
    [downloadTask resume];
}

#pragma mark - NSURLSessionDelegate
/* 下載過程中調(diào)用,用于跟蹤下載進(jìn)度
 * bytesWritten為單次下載大小
 * totalBytesWritten為當(dāng)當(dāng)前一共下載大小
 * totalBytesExpectedToWrite為文件大小
 */
//每次傳一個(gè)包 調(diào)用一次該函數(shù) 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(@"??????進(jìn)度:%f",dowProgeress);
    if (self.myDeleate && [self.myDeleate respondsToSelector:@selector(backDownprogress:tag:)]) {
        [self.myDeleate backDownprogress:dowProgeress tag:self.tag];
    }
}

/*
 2.下載完成之后調(diào)用該方法
 */
-(void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location{
    NSString *url = downloadTask.currentRequest.URL.absoluteString;
//    文件儲(chǔ)存路徑
    NSString *storagePath = [self downLoadSuccessDataDiskTmpPath:url];
    //創(chuàng)建文件管理器
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath: storagePath]) {
        //如果文件夾下有同名文件  則將其刪除
        [manager removeItemAtPath:storagePath error:nil];
    }
    NSError *saveError;
//    把緩存文件移動(dòng)到指定的沙盒路徑
    [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]) {//刪除磁盤中的緩存數(shù)據(jù)
        [manager removeItemAtPath:resumeDataPath error:nil];
    }
    if ([manager fileExistsAtPath:location.path]) {
        [manager removeItemAtPath:location.path error:nil];
    }

}
/* 在任務(wù)下載完成、下載失敗
 * 或者是應(yīng)用被殺掉后,重新啟動(dòng)應(yīng)用并創(chuàng)建相關(guān)identifier的Session時(shí)調(diào)用
 */
//下載失敗和完成都會(huì)調(diào)用,cancel時(shí)錯(cuò)誤為-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){//回調(diào)非取消時(shí)的錯(cuò)誤
            [self.myDeleate downError:error tag:self.tag];
        }
        NSData *resumeData = [error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData];
        [resumeData writeToFile:resumeDataPath atomically:NO];
        
    }else{//成功時(shí)調(diào)用
        if (self.myDeleate && [self.myDeleate respondsToSelector:@selector(backDownprogress:tag:)]) {
            [self.myDeleate backDownprogress:1 tag:self.tag];//解決后臺(tái)情況下下載完成后進(jìn)度條沒有更新的問題
        }
    }
}
/* 應(yīng)用在后臺(tái),而且后臺(tái)所有下載任務(wù)完成后,
 * 在所有其他NSURLSession和NSURLSessionDownloadTask委托方法執(zhí)行完后回調(diào),
 * 可以在該方法中做下載數(shù)據(jù)管理和UI刷新
 *最好將handleEventsForBackgroundURLSession中completionHandler保存,在該方法中待所有載數(shù)據(jù)管理和UI刷新做完后,再調(diào)用completionHandler()
 */
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
// 調(diào)用在 -application:handleEventsForBackgroundURLSession: 中保存的 handler
    NSLog(@"所有后臺(tái)任務(wù)已經(jīng)完成: %@",session.configuration.identifier);
    
}
/* 下載恢復(fù)時(shí)調(diào)用
 * 在使用downloadTaskWithResumeData:方法獲取到對(duì)應(yīng)NSURLSessionDownloadTask,
 * 并該task調(diào)用resume的時(shí)候調(diào)用
 */
- (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數(shù)據(jù)
-(NSData *)getResumeData:(NSString *)url{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSData *datas     = [fm contentsAtPath:[self resumeDataDiskTmpPath:url]];
    return datas;
}
//resumeData數(shù)據(jù)臨時(shí)路徑,在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;
}
//下載成功的數(shù)據(jù)存儲(chǔ)路徑,在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 中要實(shí)現(xiàn)- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler//在應(yīng)用處于后臺(tái),且后臺(tái)下載的所有任務(wù)完成后才會(huì)調(diào)用
 在后臺(tái)情況下才會(huì)執(zhí)行-(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;
///根據(jù)url取消下載
-(void)cancelDownloadTaskWithUrl:(NSString *)url;
///根據(jù)task取消下載
-(void)cancelDownloadTask:(NSURLSessionDownloadTask *)task;
///取消所有的下載任務(wù)
- (void)cancelAllCurrentDownLoadTasks;

@end

.m

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

@interface FileDownLoadNetwork()
/**  */
@property (nonatomic,strong) AFURLSessionManager *manager;
/** 為了解決后臺(tái)情況下下載完成后,進(jìn)度條不能及時(shí)更新的問題 ,如果AF的版本是3.0.0-3.1.0則不用使用該字典,這些版本在后臺(tái)下載完成后,progress的block能夠回調(diào),3.2.0以上的版本在后臺(tái)下載完成后,progress的block不回調(diào),不能及時(shí)更新進(jìn)度條,所以要使用該字典解決*/
@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;
}
/**
 后臺(tái)下載
 下載完成之后殺死app,再創(chuàng)建任務(wù)時(shí)taskIdentifier為1
 未下載完成就殺死app,再創(chuàng)建任務(wù)時(shí)taskIdentifier在上個(gè)taskIdentifier的基礎(chǔ)上增加,比如殺死前taskIdentifier的最大值為3,那么創(chuàng)建時(shí)taskIdentifier為4
 默認(rèn)下載
 只要?dú)⑺繿pp,再創(chuàng)建任務(wù)時(shí)taskIdentifier為1
 */
- (instancetype)init{
    self = [super init];
    if (self) {
//        配置(可以后臺(tái)下載)
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.lingxin.app"];
        configuration.timeoutIntervalForRequest = 30;
//        是否允許蜂窩網(wǎng)絡(luò)
        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(@"本地存儲(chǔ)的需要續(xù)傳的數(shù)據(jù)長(zhǎng)度為: %ld",resumeData.length);
    if (resumeData.length>0) {//斷點(diǎn)續(xù)傳
        NSLog(@"斷點(diǎn)續(xù)傳下載");
        downloadTask = [self.manager downloadTaskWithResumeData:resumeData progress:^(NSProgress * _Nonnull downloadProgress) {
            if (progressBlock) {
                progressBlock(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
                NSLog(@"jhl斷點(diǎn)續(xù)傳任務(wù)進(jìn)度:%F",(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount));
            }
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//        targetPath:緩存路徑,在沙盒里的library/cache中,下載成功后targetPath下的緩存數(shù)據(jù)會(huì)被刪除,下載的文件進(jìn)入到了返回的存儲(chǔ)路徑下
            return [NSURL fileURLWithPath:[self downLoadSuccessDataDiskTmpPath:requestUrl]];//返回文件存儲(chǔ)路徑
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//            filePath:文件存儲(chǔ)路徑
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
            if ([httpResponse statusCode] == 404) {
                [[NSFileManager defaultManager] removeItemAtURL:filePath error:nil];
            }
            if (error) {////取消也會(huì)報(bào)錯(cuò) 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新任務(wù)進(jìn)度:%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) {//取消也會(huì)報(bào)錯(cuò) statusCode為206 error的code為-999
                    failBlock(error,[httpResponse statusCode]);
                }
            }else{
                if (successBlock) {
                    successBlock(filePath,response);
                }
            }
        }];
    }
    [downloadTask resume];
    return downloadTask;
}
/**
 什么時(shí)候收到通知
 1.下載成功時(shí),此時(shí)error為nil
 2.下載失敗時(shí),如果是取消了,錯(cuò)誤code為-999,如果是網(wǎng)絡(luò)原因,錯(cuò)誤code為-1001
 3.任務(wù)還未下載完成,app強(qiáng)制退出或者閃退后,再次進(jìn)入app初始化season時(shí),此時(shí)收到通知,保存app強(qiáng)制退出或者閃退時(shí)系統(tǒng)幫忙存儲(chǔ)的resumedata,以便斷點(diǎn)續(xù)傳時(shí)使用。
 app殺死后系統(tǒng)幫忙存儲(chǔ)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) {//網(wǎng)絡(luò)原因
                
            }else if (error.code == -999){//取消時(shí)的錯(cuò)誤
                
                NSData *resumeData = [error.userInfo objectForKey:@"NSURLSessionDownloadTaskResumeData"];
                //            存儲(chǔ)強(qiáng)制退出或者閃退后系統(tǒng)幫忙存儲(chǔ)的resumedata數(shù)據(jù)
                [resumeData writeToFile:resumeDataPath atomically:NO];
            }

        }else{//下載成功
            FileDownLoadProgress progressBlock = [self.blockDic objectForKey:url];
            if (progressBlock) {
                NSLog(@"??????????:%@",[self.blockDic allValues]);
                progressBlock(1.0);//更新進(jìn)度
                [self.blockDic removeObjectForKey:url];
            }
            NSFileManager *manager = [NSFileManager defaultManager];
            if ([manager fileExistsAtPath:resumeDataPath]) {
//                移除緩存文件,只有app取消,閃退,強(qiáng)制退出時(shí)resumeDataPath路徑下的文件才會(huì)存在
                [manager removeItemAtPath:resumeDataPath error:nil];
                NSLog(@"緩沖的resumeData文件已經(jīng)被移除");
            }
        }
    }
}
//獲取resumedata數(shù)據(jù)
-(NSData *)getResumeData:(NSString *)url{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSData *datas     = [fm contentsAtPath:[self resumeDataDiskTmpPath:url]];
    return datas;
}
//resumeData數(shù)據(jù)臨時(shí)路徑,在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;
}
//下載成功的數(shù)據(jù)存儲(chǔ)路徑,在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;
}
//根據(jù)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) {
                    
                }];
            }
        }
    }
}
//根據(jù)task取消下載
-(void)cancelDownloadTask:(NSURLSessionDownloadTask *)task{
    if (task.state == NSURLSessionTaskStateRunning) {
        [task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
            //       這里也可以存儲(chǔ)resumeData,通知方法downloadData:中也可以存儲(chǔ)
        }];
    }
}
//停止當(dāng)前所有的下載任務(wù)
- (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];
}
//獲取當(dāng)前時(shí)間 下載id標(biāo)識(shí)用
- (NSString *)currentDateStr{
    NSDate *currentDate = [NSDate date];//獲取當(dāng)前時(shí)間,日期
    NSTimeInterval timeInterval = [currentDate timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.f",timeInterval];
}
/**
 AppDelegate 中要實(shí)現(xiàn)- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler//在應(yīng)用處于后臺(tái),且后臺(tái)下載的所有任務(wù)完成后才會(huì)調(diào)用
 app才能在后臺(tái)情況下,執(zhí)行通知和block代碼塊,不實(shí)現(xiàn)的話,當(dāng)app進(jìn)入前臺(tái)時(shí)才能執(zhí)行通知和block代碼塊
 */

@end

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容