iOS開發·網路請求大總結(NSURLConnection,NSURLSession,AFNetworking)

陳滿iOS發表於2018-07-10

0. 前言

iOS的開發中的網路下載方式包括NSData(最原始,實際開發基本不會用),NSURLConnection(古老又過氣的蘋果原生網路框架),NSURLSession(現在流行的蘋果網路框架),AFNetworking,SDWebImage以及基於AFNetworking的二次封裝框架例如XMNetworking,HYBNetworking等等。

NSURLConnection作為過氣的框架,作為對比了解一下還是有必要的。NSURLSession作為眾多網路相關的第三方框架基於的蘋果原生框架,更是有必要學習總結一下。作為第三方框架,AFNetworking,SDWebImage等等其實它們的老版本是基於NSURLConnection封裝而成的,後來才改成的基於NSURLSession。這些第三方框架相比原生框架封裝了快取的邏輯,比如記憶體快取,磁碟快取,操作快取等等。

0.1 最原始的網路下載 -- NSData+NSURL方式

  • 關鍵步驟 NSString-->NSURL-->NSData-->UIImage
  • 關鍵API

URLWithString dataWithContentsOfURL:url imageWithData:data

  • 下載示例
/**
 * 點選按鈕 -- 使用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];
        });
    });
}
複製程式碼

1. 過氣的蘋果原生網路框架 -- NSURLConnection

  • ① 下載完的事件採用block形式的API
//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;
複製程式碼
  • ② 下載完的事件採用delegate形式的API
/* 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;
複製程式碼
  • 呼叫示例 -- 採用block的API ①
/**
 * 點選按鈕 -- 使用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];
        
        // 可以在這裡把下載的檔案儲存
    }];

}
複製程式碼
  • 呼叫示例 -- 採用delegate的API ②
- (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;
}

複製程式碼

2. 現在的蘋果原生網路框架 -- NSURLSession

在iOS9.0之後,以前使用的NSURLConnection過期,蘋果推薦使用NSURLSession來替換NSURLConnection完成網路請求相關操作。NSURLSession的使用非常簡單,先根據會話物件建立一個請求Task,然後執行該Task即可。NSURLSessionTask本身是一個抽象類,在使用的時候,通常是根據具體的需求使用它的幾個子類。關係如下:

2.1 GET請求(NSURLRequest預設設定)

使用NSURLSession傳送GET請求的方法和NSURLConnection類似,整個過程如下:

1)確定請求路徑(一般由公司的後臺開發人員以介面文件的方式提供),GET請求引數直接跟在URL後面 2)建立請求物件(預設包含了請求頭和請求方法【GET】),此步驟可以省略 3)建立會話物件(NSURLSession) 4)根據會話物件建立請求任務(NSURLSessionDataTask) 5)執行Task 6)當得到伺服器返回的響應後,解析資料(XML|JSON|HTTP)

① 下載完的事件採用block形式
  • get請求示例1
  • 關鍵API
  • 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];
}
複製程式碼
  • get請求示例2
  • 關鍵API

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];
}
複製程式碼
② 下載完的事件採用delegate形式
  • 關鍵API

requestWithURL: sessionWithConfiguration dataTaskWithRequest:request

  • get請求發起示例
-(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];
}
複製程式碼
  • 代理關鍵API
//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);
    }
}
複製程式碼
  • 如下圖所示,get請求內部封裝了開始操作(其實是繼續),不用再像NSURLSession一樣外面resume了。
    iOS開發·網路請求大總結(NSURLConnection,NSURLSession,AFNetworking)

2.2 POST請求(需另外單獨設定request.HTTPMethod屬性)

  • post請求示例
  • 關鍵API

sharedSession requestWithURL: request.HTTPMethod = @"POST"; dataTaskWithRequest:request completionHandler:

-(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];
}
複製程式碼

3. HTTPS與HTTP的不同點

前面涉及到的GET和POST都屬於HTTP請求,現在蘋果的APP都推薦支援HTTPS,這就需要先配置一下證書,然後在NSURLSession(或者NSURLConnection但現在新的專案基本不用了)的代理方法裡面進行一些特別的操作。如果是AFNetWorking,也需要對AFHTTPRequestOperationManager物件進行一些特別的操作。

關於證書的配置,及需要的特別的操作,推薦閱讀:

  • https://www.jianshu.com/p/97745be81d64
  • https://www.jianshu.com/p/459e5471e61b
  • https://www.jianshu.com/p/4f826c6e48ed

4. AF封裝了GET和POST操作的 -- AFHTTPSessionManager

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請求呼叫棧分析

  • AFHTTPSessionManager.m
- (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;
}
複製程式碼
  • AFURLSessionManager.m
- (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;
}
複製程式碼

5. AF的GET和POST請求實現第二層 -- AFURLSessionManager

5.1 downloadTaskWithRequest: progress: destination: completionandler:

  • AFURLSessionManager.m

呼叫示例 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];
    }
}
複製程式碼

5.2 dataTaskWithRequest: completionHandler:

說明:這個NSURLSession的API容易跟AFURLSessionManager的API混淆,引數都是一個request和一個handler block。

  • NSURLSession的API是這樣的:
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
{
複製程式碼
  • AFURLSessionManager的API是這樣的,可以對比學習下:
- (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;
}
複製程式碼

6. 呼叫棧分析

初始化AFHTTPSessionManager的內部實現呼叫棧

  • [AFHTTPSessionManager initWithBaseURL:]
    • [AFHTTPSessionManager initWithBaseURL:sessionConfiguration:]
      • [AFURLSessionManager initWithSessionConfiguration:] // 呼叫了父類AFURLSessionManager的初始化方法
        • [NSURLSession sessionWithConfiguration:delegate:delegateQueue:] // 呼叫了原生類NSURLSession的初始化方法
        • [AFJSONResponseSerializer serializer]
        • [AFSecurityPolicy defaultPolicy]
        • [AFNetworkReachabilityManager sharedManager]
      • [AFHTTPRequestSerializer serializer]
      • [AFJSONResponseSerializer serializer]

AFHTTPSessionManager傳送請求的內部實現呼叫棧

  • [AFHTTPSessionManager GET:parameters:process:success:failure:]
    • [AFHTTPSessionManager dataTaskWithHTTPMethod:parameters:uploadProgress:downloadProgress:success:failure:] // 【註解1】
      • [AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:] // 獲得NSMutableURLRequest
      • [AFURLSessionManager dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:] // 【註解2】
        • [NSURLSession dataTaskWithRequest:] // 【註解3】
        • [AFURLSessionManager addDelegateForDataTask:uploadProgress:downloadProgress:completionHandler:] // 新增代理
          • [AFURLSessionManagerTaskDelegate init]
          • [AFURLSessionManager setDelegate:forTask:]
    • [NSURLSessionDataTask resume]

其中,【註解1】、【註解2】、【註解3】這三個方法得到的是同一個物件,即【註解3】中系統原生的NSURLSessionDataTask物件。所以,AF請求操作內部實現也是和原生NSURLSession操作一樣,建立task,呼叫resume傳送請求。

7. 開放問題:session與TCP連線數

請求的時候,NSURLSession的session跟TCP的個數是否有什麼關係?有人說請求同域名且共享的session會複用同一個TCP連結,否則就不復用,就一個session一個TCP連線?

關於這塊的知識可研究資料較少,且不可信,筆者日後研究到確定的答案後再更新。也歡迎讀者留下自己的見解。

不過據我觀察,可能沒那麼簡單,新的iOS11系統新增了多路TCP即Multipath-TCP,因而也為NSURLSession和NSURLSessionConfiguration提供了新的屬性multipathServiceType,以及HTTPMaximumConnectionsPerHost。下面是它們的定義:

  • NSURLSession.h
/* 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;
複製程式碼
  • NSURLSession.h
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);
複製程式碼
  • NSURLSessionConfiguration.h
/* 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;
複製程式碼

相關文章