NSURLSession在iOS7.0时被Apple提出后,虽然Apple一直对其良好的API设计大力推广。
在下面我们将会知道:
- 我们为什么从NSURLConnection转移为NSURLSession?
- NSURLSession 的基本用法
- AFN对NSURLSession的封装
2015年5月RFC 7540正式发表的下一代HTTP协议,是1999年来HTTP 1.1发布后的首个更新。相对于前一个版本,HTTP /2以快著称。如下图,对相同图片、相同服务器的下载,在不同协议下所需的时间:
ios9之后,NSURLSession开始支持HTTP/2
HTTP/2 意味着更快的网络连接速度
在苹果官方文档中,对属性** HTTPMaximumConnectionsPerHost**这样描述
Discussion
This property determines the maximum number of simultaneous connections made to each host by tasks within sessions based on this configuration.
This limit is per session, so if you use multiple sessions, your app as a whole may exceed this limit. Additionally, depending on your connection to the Internet, a session may use a lower limit than the one you specify.
The default value is 6 in macOS, or 4 in iOS.```
此属性根据此配置确定会话中的任务对每个主机的最大并发连接数,MacOS中的默认值为6,iOS中的默认值为4。
先看一个最简单的网络请求
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:URLString]];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
[dataTask resume];
NSRULSession 网络请求系统包括3方面:
session、configuration、Task
#### NSURLSession
我们从头文件入手
// 初始化Session
@property (class, readonly, strong) NSURLSession *sharedSession;
- (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration;
- (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(nullable id <NSURLSessionDelegate>)delegate delegateQueue:(nullable NSOperationQueue *)queue;
// 属性及代理
@property (readonly, retain) NSOperationQueue *delegateQueue;
@property (nullable, readonly, retain) id <NSURLSessionDelegate> delegate;
@property (readonly, copy) NSURLSessionConfiguration *configuration;
// dataTask
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request;
- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url;
// uploadTask
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData;
- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request;
// downloadTask
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request;
- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)url;
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData;
苹果也为NSURLSession 配置了block的回调版本
// NSURLSessionDataTask
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
// NSURLSessionUploadTask
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(nullable NSData *)bodyData completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
// NSURLSessionDownloadTask
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData completionHandler:(void (^)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
如何使用? 我们从网络加载一张图片
NSURLSession *session = [NSURLSession sharedSession];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:ImageURL]];
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
UIImage *image = [[UIImage alloc]initWithData:data];
}];
[sessionDataTask resume];
### NSURLSessionConfiguration
// 默认的配置 , 缓存存储在磁盘上
@property (class, readonly, strong) NSURLSessionConfiguration *defaultSessionConfiguration;
// 不会创建持久性存储的缓存.
@property (class, readonly, strong) NSURLSessionConfiguration *ephemeralSessionConfiguration;
// 允许程序在后台进行上传下载工作
- (NSURLSessionConfiguration *)backgroundSessionConfigurationWithIdentifier:(NSString *)identifier;
// 配置缓存策略
@property NSURLRequestCachePolicy requestCachePolicy;
// 请求超时
@property NSTimeInterval timeoutIntervalForRequest;
// 添加请求头
@property (nullable, copy) NSDictionary *HTTPAdditionalHeaders;
#### NSURLSessionTask
苹果并没有公开让我们使用 NSURLSessionTask ,而是只能使用它的子类 :
1. NSURLSessionDataTask 一般网络请求
2. NSURLSessionDownloadTask 下载大型文件等
3. NSURLSessionUploadTask 处理上传请求,图片或文件
Task Delegate 继承关系
NSURLSessionTask (抽象类,NSURLSessionTaskDelegate)
NSURLSessionDataTask (NSURLSessionDataDelegate)
-
NSURLSessionUploadTask (NSURLSessionDataDelegate)
- NSURLSessionDownloadTask (NSURLSessionDownloadDelegate)
NSURLSessionDelegate
NSURLSessionTaskDelegate
NSURLSessionDataDelegate
NSURLSessionDownloadDelegate
在初始化时,我们只需指定对应的delegate,这个手机避免了多次设计delegate,根据不同的task实现不同的delegate。
从头文件看各个delegate的作用
// NSURLSessionDelegate
//当一个session遇到系统错误或者未检测到的错误
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
//当请求需要认证、或者https证书认证 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;
//如果应用进入后台、这个方法会被调用。我们在这里可以对session发起的请求做各种操作。 - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session ;
// NSURLSessionTaskDelegate
/*当请求重定向的时候调用这个方法。我们必须设置一个新的NSURLRequest
对象传入completionHandler来重定向新的请求,但是当session
是background模式的时候,这个方法不会被调用。 */
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse )response
newRequest:(NSURLRequest )request
completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler;
/当请求需要认证的时候调用这个方法。如果没有实现这个代理,那么请求认证这个过程不会被调用。/ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge )challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;
/ 如果请求需要一个新的请求体时,这个方法就会被调用。比如认证失败的时候,我们可以通过这个机会从新认证。 */ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask )task
needNewBodyStream:(void (^)(NSInputStream * _Nullable bodyStream))completionHandler;
/当我们上传数据的时候,我们可以通过这个代理方法获取上传进度。 */ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask )task
didSendBodyData:(int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend;
/ 当task的统计信息收集好了以后,调用这个方法。 */ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask )task didFinishCollectingMetrics:(NSURLSessionTaskMetrics )metrics;
/当一个task出错的时候,会调用这个方法。如果error是nil,也会调用这个方法,表示task完成。/ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(nullable NSError *)error;
// NSURLSessionDownloadDelegate
/当一个下载task任务完成以后/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL )location;
/获取下载进度 */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask )downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
/重启一个下载任务。如果下载出错,NSURLSessionDownloadTaskResumeData
里面包含重新开始下载的数据。 */ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes;
参考文献
[AFNetWorking 的核心 AFURLSessionManager](https://github.com/Draveness/Analyze/blob/master/contents/AFNetworking/AFNetworking%20%E7%9A%84%E6%A0%B8%E5%BF%83%20AFURLSessionManager%EF%BC%88%E4%BA%8C%EF%BC%89.md#NSURLSession)
[AFNetWorking 源码之 AFURLSessionManager](https://juejin.im/entry/58f6d0be0ce463006bc9d919)