iOS断点续传
2018-12-24 本文已影响13人
heart_领
一、原生代码
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