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级别的事件。

 

iOS开发-AFNetworking_第1张图片 

流程图

 

你可能感兴趣的:(iOS开发-AFNetworking)