iOS開發-AFNetworking


一、通過AFHTTPSessionManager請求

1、創建manager

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

2、設置超時時間、接收類型

manager.requestSerializer.timeoutInterval = 10;
    
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", @"text/plain", nil];

3、發起http請求

[manager POST:str parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       //成功的代碼
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       //失敗的代碼
    }];

 

二、AFNetworking HTTP相關的類

AFHTTPSessionManager:AFURLSessionManager類的子類,包含很多用於HTTP的設置與方法; 

  AFHTTPRequestSerializer NSURLRequest的構造器

  AFHTTPResponseSerializer NSURLResponse的構造器

 

三、流程

1、調用post請求

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
                      progress:(void (^)(NSProgress * _Nonnull))uploadProgress
                       success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
                       failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure

2、新建一個task

- (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

3、新建一個request

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                 URLString:(NSString *)URLString
                                parameters:(id)parameters
                                     error:(NSError *__autoreleasing *)error

4、新建一個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

5、添加delegate

- (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

 

四、整體架構

1,AFURLSessionManager的mutableTaskDelegatesKeyedByTaskIdentifier屬性,存放着每一個task對應的AFURLSessionManagerTaskDelegate

2,AFURLSessionManager的NSURLSessionDelegate實現,會去調用每個task對應的AFURLSessionManagerTaskDelegate;

3,設置task的AFURLSessionManagerTaskDelegate時,用了NSLock,進行多線程同步;

任務的調度采用了工廠模式、觀察者和代理模式的應用。

工廠模式

對於HTTP協議的請求有AFHTTPRequestSerializer、AFJSONRequestSerializer、AFPropertyListRequestSerializer類,三個都依賴抽象接口AFURLRequestSerialization

對於HTTP協議的響應有

AFHTTPResponseSerializer、AFJSONResponseSerializer、AFXMLParserResponseSerializer、AFXMLDocumentResponseSerializer、AFPropertyListResponseSerializer、AFImageResponseSerializer、AFCompoundResponseSerializer

都依賴抽象接口 AFURLResponseSerialization

符合 開放-封閉原則

對增加新的請求頭、響應頭是開放的,而且增加新內容不需要修改原內容

觀察者模式

對於task的每個生命周期,都會有相應的事件發出

AFNetworkingTaskDidResumeNotification

AFNetworkingTaskDidSuspendNotification

對於task用KVO的方式去監聽Progress

代理模式

AFURLSessionManagerTaskDelegate 是AF對自己的邏輯封裝,同時實現了NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate

同時通過實現NSURLSessionDelegate,來接受session級別的事件。

 

 

流程圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM