AFURLSessionManager功能結構如下圖所示:
所有相關API結構圖:
關於https 雙向認證相關支持:
/**
開放接口,當網絡鏈接需要認證的時候調用,默認實現單向認證(客戶端認證服務器),如果需要實現雙向認證(客戶端和服務器都互相認證)實現此函數,根據蘋果掛網的配置:
https://developer.apple.com/library/mac/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html
*/
- (void)setSessionDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential))block;
3:失效session支持:
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks {
//西面的兩個函數對單例session 不起作用
if (cancelPendingTasks) {
/*invalidateAndCancel 作用同 finishTasksAndInvalidate,
cancel可以取消所有的未完成的任務,
cancellation受task的狀態約束,在調用cancel的時候,有可能許多task已經完成了*/
[self.session invalidateAndCancel];
} else {
/*存在執行中的task繼續執行,已經完成的實效,不允許創建新的task,
當失效一個background session時,在URLSession:didBecomeInvalidWithError:還沒執行之前,
不能使用相同的identifier創建新的background session*/
[self.session finishTasksAndInvalidate];
}
}
4:安全的創建task:
static void url_session_manager_create_task_safely(dispatch_block_t block) {
if (NSFoundationVersionNumber < NSFoundationVersionNumber_With_Fixed_5871104061079552_bug) {
//在併發環境中創建task,task的identifier 有可能相同,因此需要在iOSbug版本中改爲串行創建,後臺類型的session是串行順序的創建task
// Fix of bug
// Open Radar:http://openradar.appspot.com/radar?id=5871104061079552 (status: Fixed in iOS8)
// Issue about:https://github.com/AFNetworking/AFNetworking/issues/2093
dispatch_sync(url_session_manager_creation_queue(), block);
} else {
block();
}
}
5:在iOS7系統,從指定的本地文件創建上傳任務可能會爲nil,AF默認嘗試創建3次:
if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) {
for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) {
uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
}
}