iOS的開發中的網絡下載方式包括NSData(最原始,實際開發基本不會用),NSURLConnection(古老又過氣的蘋果原生網絡框架),NSURLSession(如今流行的蘋果網絡框架),AFNetworking,SDWebImage以及基於AFNetworking的二次封裝框架例如XMNetworking,HYBNetworking等等。html
NSURLConnection做爲過氣的框架,做爲對比了解一下仍是有必要的。NSURLSession做爲衆多網絡相關的第三方框架基於的蘋果原生框架,更是有必要學習總結一下。做爲第三方框架,AFNetworking,SDWebImage等等其實它們的老版本是基於NSURLConnection封裝而成的,後來才改爲的基於NSURLSession。這些第三方框架相比原生框架封裝了緩存的邏輯,好比內存緩存,磁盤緩存,操做緩存等等。ios
URLWithString dataWithContentsOfURL:url imageWithData:datamacos
/**
* 點擊按鈕 -- 使用NSData下載圖片文件,並顯示再imageView上
*/
- (IBAction)downloadBtnClick:(UIButton *)sender {
// 在子線程中發送下載文件請求
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 建立下載路徑
NSURL *url = [NSURL URLWithString:@"https://upload-images.jianshu.io/upload_images/1877784-b4777f945878a0b9.jpg"];
// NSData的dataWithContentsOfURL:方法下載
NSData *data = [NSData dataWithContentsOfURL:url];
// 回到主線程,刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithData:data];
});
});
}
複製代碼
//handler A block which receives the results of the resource load.
+ (void)sendAsynchronousRequest:(NSURLRequest*) request
queue:(NSOperationQueue*) queue
completionHandler:(void (^)(NSURLResponse* _Nullable response, NSData* _Nullable data, NSError* _Nullable connectionError)) handler API_DEPRECATED("Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h", macos(10.7,10.11), ios(5.0,9.0), tvos(9.0,9.0)) __WATCHOS_PROHIBITED;
複製代碼
/* Designated initializer */
- (nullable instancetype)initWithRequest:(NSURLRequest *)request delegate:(nullable id)delegate startImmediately:(BOOL)startImmediately API_DEPRECATED("Use NSURLSession (see NSURLSession.h)", macos(10.5,10.11), ios(2.0,9.0), tvos(9.0,9.0)) __WATCHOS_PROHIBITED;
- (nullable instancetype)initWithRequest:(NSURLRequest *)request delegate:(nullable id)delegate API_DEPRECATED("Use NSURLSession (see NSURLSession.h)", macos(10.3,10.11), ios(2.0,9.0), tvos(9.0,9.0)) __WATCHOS_PROHIBITED;
+ (nullable NSURLConnection*)connectionWithRequest:(NSURLRequest *)request delegate:(nullable id)delegate API_DEPRECATED("Use NSURLSession (see NSURLSession.h)", macos(10.3,10.11), ios(2.0,9.0), tvos(9.0,9.0)) __WATCHOS_PROHIBITED;
複製代碼
/**
* 點擊按鈕 -- 使用NSURLConnection下載圖片文件,並顯示再imageView上
*/
- (IBAction)downloadBtnClicked:(UIButton *)sender {
// 建立下載路徑
NSURL *url = [NSURL URLWithString:@"https://upload-images.jianshu.io/upload_images/1877784-b4777f945878a0b9.jpg"];
// NSURLConnection發送異步Get請求,該方法iOS9.0以後就廢除了,推薦NSURLSession
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
self.imageView.image = [UIImage imageWithData:data];
// 能夠在這裏把下載的文件保存
}];
}
複製代碼
- (IBAction)downloadBtnClicked:(UIButton *)sender {
// 建立下載路徑
NSURL *url = [NSURL URLWithString:@"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg"];
// NSURLConnection發送異步Get請求,並實現相應的代理方法,該方法iOS9.0以後廢除了。
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
}
複製代碼
#pragma mark <NSURLConnectionDataDelegate> 實現方法
/**
* 接收到響應的時候:建立一個空的沙盒文件
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 得到下載文件的總長度
self.fileLength = response.expectedContentLength;
// 沙盒文件路徑
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"QQ_V5.4.0.dmg"];
NSLog(@"File downloaded to: %@",path);
// 建立一個空的文件到沙盒中
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
// 建立文件句柄
self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
}
/**
* 接收到具體數據:把數據寫入沙盒文件中
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 指定數據的寫入位置 -- 文件內容的最後面
[self.fileHandle seekToEndOfFile];
// 向沙盒寫入數據
[self.fileHandle writeData:data];
// 拼接文件總長度
self.currentLength += data.length;
// 下載進度
self.progressView.progress = 1.0 * self.currentLength / self.fileLength;
self.progressLabel.text = [NSString stringWithFormat:@"當前下載進度:%.2f%%",100.0 * self.currentLength / self.fileLength];
}
/**
* 下載完文件以後調用:關閉文件、清空長度
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// 關閉fileHandle
[self.fileHandle closeFile];
self.fileHandle = nil;
// 清空長度
self.currentLength = 0;
self.fileLength = 0;
}
複製代碼
在iOS9.0以後,之前使用的NSURLConnection過時,蘋果推薦使用NSURLSession來替換NSURLConnection完成網路請求相關操做。NSURLSession的使用很是簡單,先根據會話對象建立一個請求Task,而後執行該Task便可。NSURLSessionTask自己是一個抽象類,在使用的時候,一般是根據具體的需求使用它的幾個子類。關係以下:緩存
使用NSURLSession發送GET請求的方法和NSURLConnection相似,整個過程以下:bash
1)肯定請求路徑(通常由公司的後臺開發人員以接口文檔的方式提供),GET請求參數直接跟在URL後面 2)建立請求對象(默認包含了請求頭和請求方法【GET】),此步驟能夠省略 3)建立會話對象(NSURLSession) 4)根據會話對象建立請求任務(NSURLSessionDataTask) 5)執行Task 6)當獲得服務器返回的響應後,解析數據(XML|JSON|HTTP)服務器
- sharedSession
- requestWithURL:
- dataTaskWithRequest:
-(void)getWithBlock1
{
//對請求路徑的說明
//http://120.25.226.186:32812/login?username=520it&pwd=520&type=JSON
//協議頭+主機地址+接口名稱+?+參數1&參數2&參數3
//協議頭(http://)+主機地址(120.25.226.186:32812)+接口名稱(login)+?+參數1(username=520it)&參數2(pwd=520)&參數3(type=JSON)
//GET請求,直接把請求參數跟在URL的後面以?隔開,多個參數之間以&符號拼接
//1.肯定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
//2.建立請求對象
//請求對象內部默認已經包含了請求頭和請求方法(GET)
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.得到會話對象
NSURLSession *session = [NSURLSession sharedSession];
//4.根據會話對象建立一個Task(發送請求)
/*
第一個參數:請求對象
第二個參數:completionHandler回調(請求完成【成功|失敗】的回調)
data:響應體信息(指望的數據)
response:響應頭信息,主要是對服務器端的描述
error:錯誤信息,若是請求失敗,則error有值
*/
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil) {
//6.解析服務器返回的數據
//說明:(此處返回的數據是JSON格式的,所以使用NSJSONSerialization進行反序列化處理)
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",dict);
}
}];
//5.執行任務
[dataTask resume];
}
複製代碼
sharedSession dataTaskWithURL:網絡
-(void)getWithBlock2
{
//1.肯定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
//2.得到會話對象
NSURLSession *session = [NSURLSession sharedSession];
//3.根據會話對象建立一個Task(發送請求)
/*
第一個參數:請求路徑
第二個參數:completionHandler回調(請求完成【成功|失敗】的回調)
data:響應體信息(指望的數據)
response:響應頭信息,主要是對服務器端的描述
error:錯誤信息,若是請求失敗,則error有值
注意:
1)該方法內部會自動將請求路徑包裝成一個請求對象,該請求對象默認包含了請求頭信息和請求方法(GET)
2)若是要發送的是POST請求,則不能使用該方法
*/
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//5.解析數據
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",dict);
}];
//4.執行任務
[dataTask resume];
}
複製代碼
requestWithURL: sessionWithConfiguration dataTaskWithRequest:requestsession
-(void) getWithDelegate1
{
//1.肯定請求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
//2.建立請求對象
//請求對象內部默認已經包含了請求頭和請求方法(GET)
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.得到會話對象,並設置代理
/*
第一個參數:會話對象的配置信息defaultSessionConfiguration 表示默認配置
第二個參數:誰成爲代理,此處爲控制器自己即self
第三個參數:隊列,該隊列決定代理方法在哪一個線程中調用,能夠傳主隊列|非主隊列
[NSOperationQueue mainQueue] 主隊列: 代理方法在主線程中調用
[[NSOperationQueue alloc]init] 非主隊列: 代理方法在子線程中調用
*/
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
//4.根據會話對象建立一個Task(發送請求)
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
//5.執行任務
[dataTask resume];
}
複製代碼
//1.接收到服務器響應的時候調用該方法
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
//2.接收到服務器返回數據的時候會調用該方法,若是數據較大那麼該方法可能會調用屢次
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
//3.當請求完成(成功|失敗)的時候會調用該方法,若是請求失敗,則error有值
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
複製代碼
#pragma mark NSURLSessionDataDelegate
//1.接收到服務器響應的時候調用該方法
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
//在該方法中能夠獲得響應頭信息,即response
NSLog(@"didReceiveResponse--%@",[NSThread currentThread]);
//注意:須要使用completionHandler回調告訴系統應該如何處理服務器返回的數據
//默認是取消的
/*
NSURLSessionResponseCancel = 0, 默認的處理方式,取消
NSURLSessionResponseAllow = 1, 接收服務器返回的數據
NSURLSessionResponseBecomeDownload = 2,變成一個下載請求
NSURLSessionResponseBecomeStream 變成一個流
*/
completionHandler(NSURLSessionResponseAllow);
}
//2.接收到服務器返回數據的時候會調用該方法,若是數據較大那麼該方法可能會調用屢次
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData--%@",[NSThread currentThread]);
//拼接服務器返回的數據
[self.responseData appendData:data];
}
//3.當請求完成(成功|失敗)的時候會調用該方法,若是請求失敗,則error有值
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"didCompleteWithError--%@",[NSThread currentThread]);
if(error == nil)
{
//解析數據,JSON解析請參考http://www.cnblogs.com/wendingding/p/3815303.html
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];
NSLog(@"%@",dict);
}
}
複製代碼
sharedSession requestWithURL: request.HTTPMethod = @"POST"; dataTaskWithRequest:request completionHandler:app
-(void)postWithBlock
{
//對請求路徑的說明
//http://120.25.226.186:32812/login
//協議頭+主機地址+接口名稱
//協議頭(http://)+主機地址(120.25.226.186:32812)+接口名稱(login)
//POST請求須要修改請求方法爲POST,並把參數轉換爲二進制數據設置爲請求體
//1.建立會話對象
NSURLSession *session = [NSURLSession sharedSession];
//2.根據會話對象建立task
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
//3.建立可變的請求對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//4.修改請求方法爲POST
request.HTTPMethod = @"POST";
//5.設置請求體
request.HTTPBody = [@"username=520it&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
//6.根據會話對象建立一個Task(發送請求)
/*
第一個參數:請求對象
第二個參數:completionHandler回調(請求完成【成功|失敗】的回調)
data:響應體信息(指望的數據)
response:響應頭信息,主要是對服務器端的描述
error:錯誤信息,若是請求失敗,則error有值
*/
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//8.解析數據
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",dict);
}];
//7.執行任務
[dataTask resume];
}
複製代碼
前面涉及到的GET和POST都屬於HTTP請求,如今蘋果的APP都推薦支持HTTPS,這就須要先配置一下證書,而後在NSURLSession(或者NSURLConnection但如今新的項目基本不用了)的代理方法裏面進行一些特別的操做。若是是AFNetWorking,也須要對AFHTTPRequestOperationManager對象進行一些特別的操做。框架
關於證書的配置,及須要的特別的操做,推薦閱讀:
AFNetworking2.0和3.0區別很大,也是由於蘋果廢棄了NSURLConnection,而改用了NSURLSession,AFNetworking3.0實際上只是對NSURLSession所作的操做進行了高度封裝,提供更加簡潔的API供編碼調用。
查看AFHTTPSessionManager.h文件,可知AFHTTPSessionManager是AFURLSessionManager的子類:
@interface AFHTTPSessionManager : AFURLSessionManager <NSSecureCoding, NSCopying>
複製代碼
請求示例 -- 下載一個PDF文件
- (void)DownloadPdfAndSave{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/pdf"];
__weak __typeof__(self) weakSelf = self;
//臨時配置,須要本身根據接口地址改動!!!!!!!!!!!!!!!!!!!!
self.urlStr = @"http://10.20.201.78/test3.pdf";
[manager GET:_urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
__strong __typeof__(weakSelf) strongSelf = weakSelf;
strongSelf.isWriten = [responseObject writeToFile:[self pathOfPdf] atomically:YES];
[strongSelf openPdfByAddingSubView];
//[strongSelf.previewController reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error.userInfo);
}];
}
複製代碼
get請求調用棧分析
- (NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
return [self GET:URLString parameters:parameters progress:nil success:success failure:failure];
}
複製代碼
- (NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(id)parameters
progress:(void (^)(NSProgress * _Nonnull))downloadProgress
success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
{
NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"GET"
URLString:URLString
parameters:parameters
uploadProgress:nil
downloadProgress:downloadProgress
success:success
failure:failure];
[dataTask resume];
return dataTask;
}
複製代碼
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
NSError *serializationError = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
if (serializationError) {
if (failure) {
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
}
return nil;
}
__block NSURLSessionDataTask *dataTask = nil;
dataTask = [self dataTaskWithRequest:request
uploadProgress:uploadProgress
downloadProgress:downloadProgress
completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) {
failure(dataTask, error);
}
} else {
if (success) {
success(dataTask, responseObject);
}
}
}];
return dataTask;
}
複製代碼
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler {
__block NSURLSessionDataTask *dataTask = nil;
url_session_manager_create_task_safely(^{
dataTask = [self.session dataTaskWithRequest:request];
});
[self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];
return dataTask;
}
複製代碼
調用示例 DownloadVC.m
- (IBAction)downloadBtnClicked:(UIButton *)sender {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
// 1. 建立會話管理者
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
// 2. 建立下載路徑和請求對象
NSURL *URL = [NSURL URLWithString:@"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
// 3.建立下載任務
/**
* 第一個參數 - request:請求對象
* 第二個參數 - progress:下載進度block
* 其中: downloadProgress.completedUnitCount:已經完成的大小
* downloadProgress.totalUnitCount:文件的總大小
* 第三個參數 - destination:自動完成文件剪切操做
* 其中: 返回值:該文件應該被剪切到哪裏
* targetPath:臨時路徑 tmp NSURL
* response:響應頭
* 第四個參數 - completionHandler:下載完成回調
* 其中: filePath:真實路徑 == 第三個參數的返回值
* error:錯誤信息
*/
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress) {
__weak typeof(self) weakSelf = self;
// 獲取主線程,否則沒法正確顯示進度。
NSOperationQueue* mainQueue = [NSOperationQueue mainQueue];
[mainQueue addOperationWithBlock:^{
// 下載進度
weakSelf.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
weakSelf.progressLabel.text = [NSString stringWithFormat:@"當前下載進度:%.2f%%",100.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount];
}];
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *path = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [path URLByAppendingPathComponent:@"QQ_V5.4.0.dmg"];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
// 4. 開啓下載任務
[downloadTask resume];
}
複製代碼
內部封裝分析 AFURLSessionManager.m
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock
destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
{
__block NSURLSessionDownloadTask *downloadTask = nil;
url_session_manager_create_task_safely(^{
downloadTask = [self.session downloadTaskWithRequest:request];
});
[self addDelegateForDownloadTask:downloadTask progress:downloadProgressBlock destination:destination completionHandler:completionHandler];
return downloadTask;
}
複製代碼
其中self.session
是AFURLSessionManager.h中的屬性
@property (readonly, nonatomic, strong) NSURLSession *session;
複製代碼
它後面調用的API聲明在NSFoundation的NSURLSession.h的頭文件中
/* Creates a data task with the given request. The request may have a body stream. */
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request;
複製代碼
添加代理的封裝 AFURLSessionManager.m
- (void)addDelegateForDataTask:(NSURLSessionDataTask *)dataTask
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init];
delegate.manager = self;
delegate.completionHandler = completionHandler;
dataTask.taskDescription = self.taskDescriptionForSessionTasks;
[self setDelegate:delegate forTask:dataTask];
delegate.uploadProgressBlock = uploadProgressBlock;
delegate.downloadProgressBlock = downloadProgressBlock;
}
複製代碼
其中
- (void)setDelegate:(AFURLSessionManagerTaskDelegate *)delegate
forTask:(NSURLSessionTask *)task
{
NSParameterAssert(task);
NSParameterAssert(delegate);
[self.lock lock];
self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)] = delegate;
[delegate setupProgressForTask:task];
[self addNotificationObserverForTask:task];
[self.lock unlock];
}
複製代碼
其中,self.mutableTaskDelegatesKeyedByTaskIdentifier
是個字典
@property (readwrite, nonatomic, strong) NSMutableDictionary *mutableTaskDelegatesKeyedByTaskIdentifier;
複製代碼
被調用的地方在:
- (AFURLSessionManagerTaskDelegate *)delegateForTask:(NSURLSessionTask *)task {
NSParameterAssert(task);
AFURLSessionManagerTaskDelegate *delegate = nil;
[self.lock lock];
delegate = self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)];
[self.lock unlock];
return delegate;
}
複製代碼
進而被調用的地方在:
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:downloadTask];
if (self.downloadTaskDidFinishDownloading) {
NSURL *fileURL = self.downloadTaskDidFinishDownloading(session, downloadTask, location);
if (fileURL) {
delegate.downloadFileURL = fileURL;
NSError *error = nil;
[[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error];
if (error) {
[[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:error.userInfo];
}
return;
}
}
if (delegate) {
[delegate URLSession:session downloadTask:downloadTask didFinishDownloadingToURL:location];
}
}
複製代碼
說明:這個NSURLSession的API容易跟AFURLSessionManager的API混淆,參數都是一個request和一個handler block。
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
{
複製代碼
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
複製代碼
調用示例 -- dataTaskWithRequest: DownloadVC.m
// 建立下載URL
NSURL *url = [NSURL URLWithString:@"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg"];
// 2.建立request請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 設置HTTP請求頭中的Range
NSString *range = [NSString stringWithFormat:@"bytes=%zd-", self.currentLength];
[request setValue:range forHTTPHeaderField:@"Range"];
__weak typeof(self) weakSelf = self;
_downloadTask = [self.manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSLog(@"dataTaskWithRequest");
// 清空長度
weakSelf.currentLength = 0;
weakSelf.fileLength = 0;
// 關閉fileHandle
[weakSelf.fileHandle closeFile];
weakSelf.fileHandle = nil;
}];
複製代碼
其中self.manager
是懶加載獲得的AFURLSessionManager
/**
* manager的懶加載
*/
- (AFURLSessionManager *)manager {
if (!_manager) {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
// 1. 建立會話管理者
_manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
}
return _manager;
}
複製代碼
內部封裝分析 AFURLSessionManager.m
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
{
return [self dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:completionHandler];
}
複製代碼
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler {
__block NSURLSessionDataTask *dataTask = nil;
url_session_manager_create_task_safely(^{
dataTask = [self.session dataTaskWithRequest:request];
});
[self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];
return dataTask;
}
複製代碼
初始化AFHTTPSessionManager的內部實現調用棧
AFHTTPSessionManager發送請求的內部實現調用棧
其中,【註解1】、【註解2】、【註解3】這三個方法獲得的是同一個對象,即【註解3】中系統原生的NSURLSessionDataTask對象。因此,AF請求操做內部實現也是和原生NSURLSession操做同樣,建立task,調用resume發送請求。
請求的時候,NSURLSession的session跟TCP的個數是否有什麼關係?有人說請求同域名且共享的session會複用同一個TCP連接,不然就不復用,就一個session一個TCP鏈接?
關於這塊的知識可研究資料較少,且不可信,筆者往後研究到肯定的答案後再更新。也歡迎讀者留下本身的看法。
不過據我觀察,可能沒那麼簡單,新的iOS11系統新增了多路TCP即Multipath-TCP,於是也爲NSURLSession和NSURLSessionConfiguration提供了新的屬性multipathServiceType
,以及HTTPMaximumConnectionsPerHost
。下面是它們的定義:
/* multipath service type to use for connections. The default is NSURLSessionMultipathServiceTypeNone */
@property NSURLSessionMultipathServiceType multipathServiceType API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);
/* The maximum number of simultanous persistent connections per host */
@property NSInteger HTTPMaximumConnectionsPerHost;
複製代碼
typedef NS_ENUM(NSInteger, NSURLSessionMultipathServiceType)
{
NSURLSessionMultipathServiceTypeNone = 0, /* None - no multipath (default) */
NSURLSessionMultipathServiceTypeHandover = 1, /* Handover - secondary flows brought up when primary flow is not performing adequately. */
NSURLSessionMultipathServiceTypeInteractive = 2, /* Interactive - secondary flows created more aggressively. */
NSURLSessionMultipathServiceTypeAggregate = 3 /* Aggregate - multiple subflows used for greater bandwitdh. */
} API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos) NS_SWIFT_NAME(URLSessionConfiguration.MultipathServiceType);
複製代碼
/* multipath service type to use for connections. The default is NSURLSessionMultipathServiceTypeNone */
@property NSURLSessionMultipathServiceType multipathServiceType API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos);
/* The maximum number of simultanous persistent connections per host */
@property NSInteger HTTPMaximumConnectionsPerHost;
複製代碼