【IOS】AFURLSessionManager - API

// 创建NSURLSession这个对象,而NSURLSession又基于NSURLSessionConfiguration
@interface AFURLSessionManager : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSSecureCoding, NSCopying>
// 管理的NSURLSession会话对象
@property (readonly, nonatomic, strong) NSURLSession *session;
// 为NSURLSession绑定一个队列,并且设置这个队列的最大并发数
@property (readonly, nonatomic, strong) NSOperationQueue *operationQueue;
// 序列化响应数据的对象,默认是AFJSONResponseSerializer
@property (nonatomic, strong) id <AFURLResponseSerialization> responseSerializer;
//安全策略,默认defaultPolicy
@property (nonatomic, strong) AFSecurityPolicy *securityPolicy;
#if !TARGET_OS_WATCH
// 网络可达性,默认sharedManager
@property (readwrite, nonatomic, strong) AFNetworkReachabilityManager *reachabilityManager;
#endif
// 被管理session的data、upload、download任务集合
@property (readonly, nonatomic, strong) NSArray <NSURLSessionTask *> *tasks;
// data任务集合
@property (readonly, nonatomic, strong) NSArray <NSURLSessionDataTask *> *dataTasks;
// upload任务集合
@property (readonly, nonatomic, strong) NSArray <NSURLSessionUploadTask *> *uploadTasks;
// download任务集合
@property (readonly, nonatomic, strong) NSArray <NSURLSessionDownloadTask *> *downloadTasks;
// 回调队列,请求完成后,回调block会在这个队列中调用,如果为空,就在主队列
@property (nonatomic, strong, nullable) dispatch_queue_t completionQueue;
// 请求成功后,回调block会在这个组中调用,如果为空,就使用一个私有的组
@property (nonatomic, strong, nullable) dispatch_group_t completionGroup;
// 这个属性用来解决在后台创建上传任务返回nil的bug,默认为NO,如果为YES,在后台创建上传任务失败后,会尝试重新创建该任务
@property (nonatomic, assign) BOOL attemptsToRecreateUploadTasksForBackgroundSessions;
// 初始化方法
- (instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
// 根据是否取消未完成的任务来使session失效,NSURLSession有两个方法:finishTasksAndInvalidate(等待所有任务完成后失效) invalidateAndCancel(立即失效)
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks;
// 使用指定的请求创建NSURLSessionDataTask
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
                            completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject,  NSError * _Nullable error))completionHandler DEPRECATED_ATTRIBUTE;
// 使用指定的请求创建NSURLSessionDataTask
- (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;
// 使用 本地文件 创建NSURLSessionUploadTask
- (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;
// 使用 bodyData 创建NSURLSessionUploadTask
- (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
- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request
                                                 progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock
                                        completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler;
// 使用指定的请求创建一个NSURLSessionDownloadTask
- (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
- (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;
// 返回指定任务的上传过程
- (nullable NSProgress *)uploadProgressForTask:(NSURLSessionTask *)task;
// 返回指定任务的下载进度
- (nullable NSProgress *)downloadProgressForTask:(NSURLSessionTask *)task;
// 设置当托管会话无效时要执行的块,由NSURLSessionDelegate 的 URLSession:didBecomeInvalidWithError处理
- (void)setSessionDidBecomeInvalidBlock:(nullable void (^)(NSURLSession *session, NSError *error))block;
// 设置当发生连接级别身份验证挑战是要执行的块, 如 NSURLSessionDelegate 的 URLSession:didReceiveChallenge:completionHandler 处理的那样
- (void)setSessionDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential))block;
// 当任务需要一个新的请求体发送到远程服务器时执行的块方法, 由 NSURLSessionTaskDelegate 的 URLSession:task:needNewBodyStream 处理
- (void)setTaskNeedNewBodyStreamBlock:(nullable NSInputStream * (^)(NSURLSession *session, NSURLSessionTask *task))block;
// 设置当HTTP请求视图执行重定向到另一个URL时执行的块方法, 由 NSURLSessionTaskDelegate 的 URLSession:willPerformHTTPRedirection:newRequest:completionHandler 处理
- (void)setTaskWillPerformHTTPRedirectionBlock:(nullable NSURLRequest * _Nullable (^)(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request))block;
// 设置当会话任务接收到特定与请求的身份验证时要执行的块方法, 由 NSURLSessionTaskDelegate URLSession:task:didReceiveChallenge:completionHandler 处理
- (void)setTaskDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLSessionTask *task, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential))block;
// 设置一个要定期执行的块来跟踪上传进度, 由 NSURLSessionTaskDelegate 的 URLSession:task:didSendBodyData:totalBytesSend:totalBytesExpectedToSend 处理
- (void)setTaskDidSendBodyDataBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend))block;
// 将一个要执行的块设置为与特定任务相关的最后一条消息,由 NSURLSessionTaskDelegate 的 URLSession:task:didCompleteWithError: 处理
- (void)setTaskDidCompleteBlock:(nullable void (^)(NSURLSession *session, NSURLSessionTask *task, NSError * _Nullable error))block;
// 设置当数据任务收到响应时要执行的块,由 NSURLSessionDataDelegate 的 URLSession:dataTask:didReceiveResponse:completionHandler 处理
- (void)setDataTaskDidReceiveResponseBlock:(nullable NSURLSessionResponseDisposition (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response))block;
// 设置当数据任务变成下载任务时要执行的块方法, 由 NSURLSessionDataDelegate 的 URLSession:dataTask:didBecomeDownloadTask 处理
- (void)setDataTaskDidBecomeDownloadTaskBlock:(nullable void (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask))block;
// 设置数据任务接收到数据时要执行的块方法,由NSURLSessionDataDelegate 的 URLSession:dataTask:didReceiveData 处理
- (void)setDataTaskDidReceiveDataBlock:(nullable void (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data))block;
// 设置要执行的块,已确定数据任务的缓存行为, 如 NSURLSessionDataDelegate 的 URLSession:dataTask:willCacheResponse:completionHandler 那样
- (void)setDataTaskWillCacheResponseBlock:(nullable NSCachedURLResponse * (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse))block;
// 设置一个块方法,当会话排队的所有消息完成时,该块方法被执行 如 NSURLSessionDataDelegate 的 URLSessionDidFinishEventsForBackgroundURLSession 那样
- (void)setDidFinishEventsForBackgroundURLSessionBlock:(nullable void (^)(NSURLSession *session))block AF_API_UNAVAILABLE(macos);
// 设置下载任务完成下载后要执行的块方法,如 NSURLSessionDownloadDelegate 的 URLSession:downloadTask:didFinishDownloadingToURL方法
- (void)setDownloadTaskDidFinishDownloadingBlock:(nullable NSURL * _Nullable  (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block;
// 设置一个要定期执行的块来跟踪下载进度, 如 NSURLSessionDownloadDelegate 的 URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesWritten:totalBytesExpectedToWrite 方法
- (void)setDownloadTaskDidWriteDataBlock:(nullable void (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite))block;
// 设置下载任务恢复后要执行的块方法, 如 NSURLSessionDownloadDelegate 的 URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes 方法
- (void)setDownloadTaskDidResumeBlock:(nullable void (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t fileOffset, int64_t expectedTotalBytes))block;
@end

posted @ 2019-06-30 20:53  220和284  阅读(252)  评论(0编辑  收藏  举报