基础应用

AFNetworking详解之AFURLSessionManag

2017-04-24  本文已影响0人  my_杨哥
AFNetworking

AFURLSessionManager是一个可以请求数据、上传数据、下载数据的类。

一、属性

//会话对象
@property (readonly, nonatomic, strong) NSURLSession *session;
//代理回调所运行的 操作队列
@property (readonly, nonatomic, strong) NSOperationQueue *operationQueue;
//安全相关(策略)
@property (nonatomic, strong) AFSecurityPolicy *securityPolicy;
//网络状态监测管理者
@property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;

Session 就是该类要管理的 NSURLSession 对象;
operationQueue是操作队列,当代理回调的时候运行;
securityPolicy是用于处理网络连接安全处理策略;
reachabilityManager是检测网络状态的检测器。

//任务(抽象类,本身不能使用,只能使用它的子类)
@property (readonly, nonatomic, strong) NSArray <NSURLSessionTask *> *tasks;
//(数据)任务,一般用来处理网络请求,如(GET|POST)请求。或 用于(断点下载|支持离线)
@property (readonly, nonatomic, strong) NSArray <NSURLSessionDataTask *> *dataTasks;
//(上传)任务,用于处理上传请求
@property (readonly, nonatomic, strong) NSArray <NSURLSessionUploadTask *> *uploadTasks;
//(下载)任务,用于处理下载请求
@property (readonly, nonatomic, strong) NSArray <NSURLSessionDownloadTask *> *downloadTasks;

通过这四个属性,我们分别可以拿到总的任务集合、数据任务集合、上传任务集合和下载任务集合

@property (nonatomic, assign) BOOL attemptsToRecreateUploadTasksForBackgroundSessions;

这个属性非常重要,注释里面写到,在iOS7中存在一个bug,在创建后台上传任务时,有时候会返回nil,所以为了解决这个问题,AFNetworking遵照了苹果的建议,在创建失败的时候,会重新尝试创建,次数默认为3次,所以你的应用如果有场景会有在后台上传的情况的话,记得将该值设为YES,避免出现上传失败的问题

二、初始化

- (instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;

被指定的初始化方法

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks;

如果将cancelPendingTasks设为YES的话,会在主线程直接关闭掉当前会话,NO的话,会等待当前task结束后再关闭

三、核心方法

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
                            completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler DEPRECATED_ATTRIBUTE;
- (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;

上面两种方法是数据请求任务,
request则是你发出的HTTP请求
uploadProgressBlockdownloadProgressBlock则是在如果上传和下载进度有更新的情况下才会调用
completionHandler就是在请求结束之后返回的内容
DEPRECATED_ATTRIBUTE 标识符的意思是慢慢弃用的属性或接口

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromFile:(NSURL *)fileURL
                                         progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
                                completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError  * _Nullable error))completionHandler;
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromData:(nullable NSData *)bodyData
                                         progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
                                completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler;
- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request
                                                 progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
                                        completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler;

上面三种不同的数据上传方法
第一种是 通过fileURL(需要上传的本地文件URL路径)上传,
第二种是 通过bodyData(需要上传的HTTP body体的数据),
第三种是 使用流(输出流)请求的方法,在使用该方法的时候,一定要设置setTaskNeedNewBodyStreamBlock回调,否则session没办法在重新发送steam的时候找到数据源。

- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
                                             progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock
                                          destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
                                    completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * _Nullable filePath, NSError * _Nullable error))completionHandler;
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData
                                                progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock
                                             destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
                                       completionHandler:(nullable void (^)(NSURLResponse *response, NSURL * _Nullable filePath, NSError * _Nullable error))completionHandler;

上面是两种下载方法,
第一种 Request 是通过HTTP请求方式下载;
第二种 ResumeData 则是通过之前的下载数据来恢复下载;
destination在下载的过程中文件会先存放在一个临时的位置,等到下载完成之后,文件会转移到目标位置

四、Notifications

FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidResumeNotification;

在对外提供的notification key里面,使用了FOUNDATION_EXPORT来定义常量,使用FOUNDATION_EXPORTextern或者define有什么区别呢?
FOUNDATION_EXPORT在c文件编译下是和extern等同,在c++文件编译下是和extern “C”等同,在32位机的环境下又是另外编译情况,在兼容性方面,FOUNDATION_EXPORT做的会更好。

上一篇下一篇

猜你喜欢

热点阅读