IOS 開發 網路詳解(十一) AFURLSessionManager

來源:互聯網
上載者:User

標籤:each   count   詳解   dma   ntop   ogre   cat   response   shared   

AFURLSessionManager 絕對可以稱得上是 AFNetworking 的核心。負責建立和管理 NSURLSession管理 NSURLSessionTask實現 NSURLSessionDelegate 等協議中的代理方法使用 AFURLSessionManagerTaskDelegate 管理進度使用 _AFURLSessionTaskSwizzling 調劑方法引入 AFSecurityPolicy 保證請求的安全引入 AFNetworkReachabilityManager 監控網路狀態
1.建立
在初始化方法中,需要完成初始化一些自己持有的執行個體:初始化會話配置(NSURLSessionConfiguration),預設為 defaultSessionConfiguration初始化會話(session),並設定會話的代理以及代理隊列初始化管理響應序列化(AFJSONResponseSerializer),安全認證(AFSecurityPolicy)以及監控網路狀態(AFNetworkReachabilityManager)的執行個體初始化儲存 data task 的字典(mutableTaskDelegatesKeyedByTaskIdentifier)
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {    self = [super init];    if (!self) {        return nil;    }    if (!configuration) {        configuration = [NSURLSessionConfiguration defaultSessionConfiguration];    }    self.sessionConfiguration = configuration;    self.operationQueue = [[NSOperationQueue alloc] init];    self.operationQueue.maxConcurrentOperationCount = 1;    self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];    self.responseSerializer = [AFJSONResponseSerializer serializer];    self.securityPolicy = [AFSecurityPolicy defaultPolicy];    self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];    self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];    self.lock = [[NSLock alloc] init];    self.lock.name = AFURLSessionManagerLockName;    #1: 為已有的 task 設定代理, 略    return self;}

 

2.管理
- (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 *)uploadTaskWithRequest:(NSURLRequest *)request                                         fromFile:(NSURL *)fileURL                                         progress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock                                completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError  * _Nullable error))completionHandler;...- (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;

 

- (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;}
調用 - [NSURLSession dataTaskWithRequest:] 方法傳入 NSURLRequest調用 - [AFURLSessionManager addDelegateForDataTask:uploadProgress:downloadProgress:completionHandler:] 方法返回一個 AFURLSessionManagerTaskDelegate 對象將 completionHandler uploadProgressBlock 和 downloadProgressBlock 傳入該對象並在相應事件發生時進行回調

 

3.實現NSURLSesstion的delegate
- (void)setSessionDidBecomeInvalidBlock:(nullable void (^)(NSURLSession *session, NSError *error))block;- (void)setSessionDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential))block;...
- (void)setSessionDidBecomeInvalidBlock:(void (^)(NSURLSession *session, NSError *error))block {    self.sessionDidBecomeInvalid = block;}

 

IOS 開發 網路詳解(十一) AFURLSessionManager

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.