ios断点续传:通过NSURLSession实现文件下载功能

NSURLConnection在iOS9被宣布弃用,NSURLSession是苹果在iOS7后为HTTP数据传输提供的一系列接口,比NSURLConnection强大,坑少,好用.今天从使用的角度介绍下.

使用NSURLSession,拢共分两步:

  • 第一步 通过NSURLSession的实例创建task
  • 第二部 执行task

既然两步里面都出现了task,就先说说它吧.
NSURLSessionTask可以简单理解为任务:如数据请求任务,下载任务,上传任务and so on.我们使用的是他的子类们:

  • NSURLSessionTask(抽象类)
    • NSURLSessionDataTask
      • NSURLSessionUploadTask
    • NSURLSessionDownloadTask

从这几个子类的名字就可以大概猜出他们的作用了.接下来我们就从不同类型的任务出发,来使用session.

NSURLSessionDataTask

字面上看是和数据相关的任务,但其实dataTask完全可以胜任downloadTask和uploadTask的工作.这可能也是我们使用最多的task种类.

简单GET请求

如果请求的数据比较简单,也不需要对返回的数据做一些复杂的操作.那么我们可以使用带block

// 快捷方式获得session对象
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"http://www.daka.com/login?username=daka&pwd=123"];
// 通过URL初始化task,在block内部可以直接对返回的数据进行处理
NSURLSessionTask *task = [session dataTaskWithURL:url
                               completionHandler:^(NSData *data, NSURLResponse *response, NSError error) {
    NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
}];

// 启动任务
[task resume];

所有类型的task都要调用resume方法才会开始进行请求.

简单POST请求

POST和GET的区别就在于request,所以使用session的POST请求和GET过程是一样的,区别就在于对request的处理.

NSURL *url = [NSURL URLWithString:@"http://www.daka.com/login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"username=daka&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];

NSURLSession *session = [NSURLSession sharedSession];
// 由于要先对request先行处理,我们通过request初始化task
NSURLSessionTask *task = [session dataTaskWithRequest:request
                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }];
[task resume];

NSURLSessionDataDelegate代理方法

NSURLSession提供了block方式处理返回数据的简便方式,但如果想要在接收数据过程中做进一步的处理,仍然可以调用相关的协议方法.NSURLSession的代理方法和NSURLConnection有些类似,都是分为接收响应、接收数据、请求完成几个阶段.

// 使用代理方法需要设置代理,但是session的delegate属性是只读的,要想设置代理只能通过这种方式创建session
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
                                                      delegate:self
                                                 delegateQueue:[[NSOperationQueue alloc] init]];

// 创建任务(因为要使用代理方法,就不需要block方式的初始化了)
NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.daka.com/login?userName=daka&pwd=123"]]];

// 启动任务
[task resume];

//对应的代理方法如下:

// 1.接收到服务器的响应
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
    // 允许处理服务器的响应,才会继续接收服务器返回的数据
    completionHandler(NSURLSessionResponseAllow);
}

// 2.接收到服务器的数据(可能调用多次)
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    // 处理每次接收的数据
}

// 3.请求成功或者失败(如果失败,error有值)
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
    // 请求完成,成功或者失败的处理
}


关键点在代码注释里面都有提及,重要的地方再强调一下:

  • 如果要使用代理方法,需要设置代理,但从NSURLSession的头文件发现session的delegate属性是只读的.因此设置代理要通过session的初始化方法赋值:sessionWithConfiguration:delegate:delegateQueue:其中:
    • configuration参数(文章开始提到的)需要传递一个配置,我们暂且使用默认的配置[NSURLSessionConfiguration defaultSessionConfiguration]就好(后面会说下这个配置是干嘛用的);
    • delegateQueue参数表示协议方法将会在哪个队列(NSOperationQueue)里面执行.
  • NSURLSession在接收到响应的时候要先对响应做允许处理:completionHandler(NSURLSessionResponseAllow);,才会继续接收服务器返回的数据,进入后面的代理方法.值得一提的是,如果在接收响应的时候需要对返回的参数进行处理(如获取响应头信息等),那么这些处理应该放在前面允许操作的前面.
下面主要介绍一下在实际项目中下载功能的实现

.h文件

#import <Foundation/Foundation.h>
#import "FileModel.h" //文件的模型
#import "FileData.h" //文件的数据库操作(下载过程中存储下载状态及信息,下载完成后存储下载文件的存储路径,并更新相关下载状态)

@interface FileDownLoad : NSObject<NSURLSessionDownloadDelegate>

//+(FileDownLoad*)sharededFileDownLoad;
/**下载对象的ID  必须和下载文件的ID一致*/
@property (nonatomic,strong) NSString * downloadID;
@property (nonatomic,strong) NSString * downloadPath;
@property (nonatomic,assign) long long currentSize;
@property (nonatomic,assign) long long maxSize;
@property (nonatomic,strong) FileModel * fileModel;
@property (nonatomic,strong) NSURLSessionDownloadTask * task;
@property (nonatomic,strong) NSData * resumeData;
@property (nonatomic,strong) NSURLSession * session;
/**开始下载*/
- (void)startDownLoad;
/**暂停下载*/
- (void)stopDownLoad:(FileModel*)model;
/**恢复下载*/
- (void)resumeDownLoad:(FileModel*)model;

@end

.m 文件

- (NSURLSession*)session{
    if (!_session) {
        NSURLSessionConfiguration * cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
        self.session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
        
    }

    return _session;
}

- (void)stopDownLoad:(FileModel*)model{

    NSLog(@"pppppppppp==%@======%@",model.fileID,self.fileModel.fileID);
    NSLog(@"停止下载");

    if ([model.fileID isEqualToString:self.fileModel.fileID]) {
        __weak typeof(self) vc = self;
        
        [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
            vc.resumeData = resumeData;
            vc.task = nil;
            self.fileModel.resumeData = resumeData;
        }];
        FileData * fileData = [FileData sharedFileData];
        [fileData deleteFileWithFileID:self.fileModel.fileID];
        [fileData insertFileModel:self.fileModel];

        
    }


}
- (void)resumeDownLoad:(FileModel*)model{
    NSLog(@"继续下载");

    
    if ([model.fileID isEqualToString:self.fileModel.fileID]) {
        if (self.resumeData == nil) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"resumeDownload" object:self.fileModel];
            return;
        }
        self.task = [self.session downloadTaskWithResumeData:self.resumeData];
        [self.task resume];
        self.resumeData = nil;
        
    }

}

- (void)startDownLoad{
    

    FileData * fileData = [FileData sharedFileData];
    if ([fileData isExistFile:self.fileModel.fileID]) {
        [fileData deleteFileWithFileID:self.fileModel.fileID];
    }
    [fileData insertFileModel:self.fileModel];
    NSURL * url = [NSURL URLWithString:self.downloadPath];
    self.task = [self.session downloadTaskWithURL:url];
    
    [self.task resume];
    
    
}
#pragma mark - NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
    
    NSString * caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask , YES) lastObject];
    NSString * file = [caches stringByAppendingString:downloadTask.response.suggestedFilename];

    NSFileManager * mager = [NSFileManager defaultManager];
    [mager moveItemAtPath:location.path toPath:file error:nil];
    self.fileModel.savePath = file;
    self.fileModel.isDownLoading = @"isOver";
    //提前存数据库
    if (![self.downloadID isEqualToString:self.fileModel.fileID]) {
        return;
    }
    FileData * fileData = [FileData sharedFileData];
    [fileData deleteFileWithFileID:self.fileModel.fileID];
    [fileData insertFileModel:self.fileModel];
    
    
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    //didWriteData为已经下载好的数据,totalBytesWritten为要下载的总数据
    self.maxSize = totalBytesExpectedToWrite;
    self.currentSize = totalBytesWritten;
    self.fileModel.currentSizePath = [NSString stringWithFormat:@"%lld",self.currentSize];
    self.fileModel.maxSizePath = [NSString stringWithFormat:@"%lld",self.maxSize];
    NSDictionary * dict = @{@"max":[NSString stringWithFormat:@"%lld",self.maxSize],@"current":[NSString stringWithFormat:@"%lld",self.currentSize],@"fileID":self.fileModel.fileID};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"download" object:dict];
    
    NSLog(@"GGGGGGGGGGGGGGGGGG=%lld/%lld",self.currentSize/1024/1024,self.maxSize/1024/1024);
    
}
//这俩个代理方法必须实现,但是在下载过程中不需要执行任何操作
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{
}
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL{
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值