伴随着iOS的发展,iOS系统的网络请求类在逐渐的完善,iOS9以后弃用了NSURLConnection,用NSURLSession来封装网络请求。接下来,就NSURLSession中常用的类来做下解读。
NSURLSession会涉及两个重要的类NSURLSessionConfiguration和NSURLSessionTask
使用NSURLSession创建网络请求分为两步:
- 通过NSURLSession实例创建task(NSURLSessionTask)
- 启动task
NSURLSessionTask可以简单理解为任务,如下载,上传等。
我们一般会使用NSURLSessionTask的子类:
NSURLSessionDataTask NSURLSessionUploadTask NSURLSessionDownloadTask
NSURLSessionDataTask 从字面上看是与数据相关的任务,其实完全可以胜任下载和上传的任务,平时程序中用的最多。
以下就用下载作为例子讲解:
1.初始化session
#pragma mark - init
- (instancetype)initWithURL:(NSURL *)url downloadPath:(NSString *)pathToDL delegate:(id)delegateOrNil
{
if (self = [super init]) {
self.downloadURL = url;
self.delegate = delegateOrNil;
self.pathToDownloadDirectory = pathToDL;
self.fileRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kDefaultRequestTimeout];
self.sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
self.sessionConfiguration.timeoutIntervalForRequest = kDefaultRequestTimeout;
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 5;
self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
}
return self;
}
2.开始下载
#pragma mark - NSOperation override
- (void)start
{
if (![NSURLConnection canHandleRequest:self.fileRequest]) {
NSError *error = [NSError errorWithDomain:YYDownloadErrorDomain code:YYDownloadErrorInvalidURL userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Invaild URL provided: %@",self.fileRequest.URL]}];
[self notifyFromCompletionWithError:error pathToFile:nil];
return;
}
NSFileManager *fm = [NSFileManager defaultManager];
//创建一个下载目录
NSError *err = nil;
if (![fm createDirectoryAtPath:self.pathToDownloadDirectory withIntermediateDirectories:YES attributes:nil error:&err]) {
[self notifyFromCompletionWithError:err pathToFile:nil];
return;
}
//检查文件是否已经存在,如果存在设置HTTP `bytes` header
if (![fm fileExistsAtPath:self.pathToFile]) {
[fm createFileAtPath:self.pathToFile contents:nil attributes:nil];
}else{
uint64_t fileSize = [[fm attributesOfItemAtPath:self.pathToFile error:nil] fileSize];
NSString *range = [NSString stringWithFormat:@"byte=%lld-",fileSize];
[self.fileRequest setValue:range forHTTPHeaderField:@"Range"];
self.receivedDataLength += fileSize;
}
self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.pathToFile];
self.receivedDataBuffer = [[NSMutableData alloc] init];
self.samplesOfDownloadedBytes = [NSMutableArray array];
self.dataTask = [self.session dataTaskWithRequest:self.fileRequest];
if (self.dataTask) {
[self willChangeValueForKey:@"isExecuting"];
self.state = YYDownloadStateDownloading;
[self didChangeValueForKey:@"isExecuting"];
[self.fileHandle seekToEndOfFile];
//调用resume,才开始请求
[self.dataTask resume];
}
}
3.NSURLSessionDataTask代理方法处理
// 1.接收到服务器的响应
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
// 允许处理服务器的响应,才会继续接收服务器返回的数据
completionHandler(NSURLSessionResponseAllow);
//计算期望接收数据长度
self.expectedDataLength = self.receivedDataLength + [response expectedContentLength];
//写数据准备工作
}
// 2.接收到服务器的数据(可能调用多次)
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
// 处理每次接收的数据
[self.receivedDataBuffer appendData:data];
self.receivedDataLength += data.length;
if (self.receivedDataBuffer && self.receivedDataBuffer.length > kBufferSize && [self isExecuting]) {
//数据写入
[self.fileHandle writeData:self.receivedDataBuffer];
//清缓存
[self.receivedDataBuffer setData:[NSData data]];
}
dispatch_async(dispatch_get_main_queue(), ^{
//下载进度block或代理方法(二选一)
if (self.progressBlock) {
self.progressBlock(self.receivedDataLength, self.expectedDataLength, self.remainingTime, self.progress);
}
if ([self.delegate respondsToSelector:@selector(download:didReceiveData:onTotal:progress:)]) {
[self.delegate download:self
didReceiveData:self.receivedDataLength
onTotal:self.expectedDataLength
progress:self.progress];
}
});
}
// 3.请求成功或者失败(如果失败,error有值)
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
// 请求完成,成功或者失败的处理
}
ps这里只抽取一些方法做讲解,详细请进入 https://github.com/caoyuanyuan/YYDownload.git查看。