AFURLSessionManager.m 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. // AFURLSessionManager.m
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import "AFURLSessionManager.h"
  22. #import <objc/runtime.h>
  23. #ifndef NSFoundationVersionNumber_iOS_8_0
  24. #define NSFoundationVersionNumber_With_Fixed_5871104061079552_bug 1140.11
  25. #else
  26. #define NSFoundationVersionNumber_With_Fixed_5871104061079552_bug NSFoundationVersionNumber_iOS_8_0
  27. #endif
  28. static dispatch_queue_t url_session_manager_creation_queue() {
  29. static dispatch_queue_t af_url_session_manager_creation_queue;
  30. static dispatch_once_t onceToken;
  31. dispatch_once(&onceToken, ^{
  32. af_url_session_manager_creation_queue = dispatch_queue_create("com.alamofire.networking.session.manager.creation", DISPATCH_QUEUE_SERIAL);
  33. });
  34. return af_url_session_manager_creation_queue;
  35. }
  36. static void url_session_manager_create_task_safely(dispatch_block_t block) {
  37. if (NSFoundationVersionNumber < NSFoundationVersionNumber_With_Fixed_5871104061079552_bug) {
  38. // Fix of bug
  39. // Open Radar:http://openradar.appspot.com/radar?id=5871104061079552 (status: Fixed in iOS8)
  40. // Issue about:https://github.com/AFNetworking/AFNetworking/issues/2093
  41. dispatch_sync(url_session_manager_creation_queue(), block);
  42. } else {
  43. block();
  44. }
  45. }
  46. static dispatch_queue_t url_session_manager_processing_queue() {
  47. static dispatch_queue_t af_url_session_manager_processing_queue;
  48. static dispatch_once_t onceToken;
  49. dispatch_once(&onceToken, ^{
  50. af_url_session_manager_processing_queue = dispatch_queue_create("com.alamofire.networking.session.manager.processing", DISPATCH_QUEUE_CONCURRENT);
  51. });
  52. return af_url_session_manager_processing_queue;
  53. }
  54. static dispatch_group_t url_session_manager_completion_group() {
  55. static dispatch_group_t af_url_session_manager_completion_group;
  56. static dispatch_once_t onceToken;
  57. dispatch_once(&onceToken, ^{
  58. af_url_session_manager_completion_group = dispatch_group_create();
  59. });
  60. return af_url_session_manager_completion_group;
  61. }
  62. NSString * const AFNetworkingTaskDidResumeNotification = @"com.alamofire.networking.task.resume";
  63. NSString * const AFNetworkingTaskDidCompleteNotification = @"com.alamofire.networking.task.complete";
  64. NSString * const AFNetworkingTaskDidSuspendNotification = @"com.alamofire.networking.task.suspend";
  65. NSString * const AFURLSessionDidInvalidateNotification = @"com.alamofire.networking.session.invalidate";
  66. NSString * const AFURLSessionDownloadTaskDidFailToMoveFileNotification = @"com.alamofire.networking.session.download.file-manager-error";
  67. NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey = @"com.alamofire.networking.task.complete.serializedresponse";
  68. NSString * const AFNetworkingTaskDidCompleteResponseSerializerKey = @"com.alamofire.networking.task.complete.responseserializer";
  69. NSString * const AFNetworkingTaskDidCompleteResponseDataKey = @"com.alamofire.networking.complete.finish.responsedata";
  70. NSString * const AFNetworkingTaskDidCompleteErrorKey = @"com.alamofire.networking.task.complete.error";
  71. NSString * const AFNetworkingTaskDidCompleteAssetPathKey = @"com.alamofire.networking.task.complete.assetpath";
  72. static NSString * const AFURLSessionManagerLockName = @"com.alamofire.networking.session.manager.lock";
  73. static NSUInteger const AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask = 3;
  74. static void * AFTaskStateChangedContext = &AFTaskStateChangedContext;
  75. typedef void (^AFURLSessionDidBecomeInvalidBlock)(NSURLSession *session, NSError *error);
  76. typedef NSURLSessionAuthChallengeDisposition (^AFURLSessionDidReceiveAuthenticationChallengeBlock)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential);
  77. typedef NSURLRequest * (^AFURLSessionTaskWillPerformHTTPRedirectionBlock)(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request);
  78. typedef NSURLSessionAuthChallengeDisposition (^AFURLSessionTaskDidReceiveAuthenticationChallengeBlock)(NSURLSession *session, NSURLSessionTask *task, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential);
  79. typedef void (^AFURLSessionDidFinishEventsForBackgroundURLSessionBlock)(NSURLSession *session);
  80. typedef NSInputStream * (^AFURLSessionTaskNeedNewBodyStreamBlock)(NSURLSession *session, NSURLSessionTask *task);
  81. typedef void (^AFURLSessionTaskDidSendBodyDataBlock)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend);
  82. typedef void (^AFURLSessionTaskDidCompleteBlock)(NSURLSession *session, NSURLSessionTask *task, NSError *error);
  83. typedef NSURLSessionResponseDisposition (^AFURLSessionDataTaskDidReceiveResponseBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response);
  84. typedef void (^AFURLSessionDataTaskDidBecomeDownloadTaskBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask);
  85. typedef void (^AFURLSessionDataTaskDidReceiveDataBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data);
  86. typedef NSCachedURLResponse * (^AFURLSessionDataTaskWillCacheResponseBlock)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse);
  87. typedef NSURL * (^AFURLSessionDownloadTaskDidFinishDownloadingBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location);
  88. typedef void (^AFURLSessionDownloadTaskDidWriteDataBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite);
  89. typedef void (^AFURLSessionDownloadTaskDidResumeBlock)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t fileOffset, int64_t expectedTotalBytes);
  90. typedef void (^AFURLSessionTaskProgressBlock)(NSProgress *);
  91. typedef void (^AFURLSessionTaskCompletionHandler)(NSURLResponse *response, id responseObject, NSError *error);
  92. #pragma mark -
  93. @interface AFURLSessionManagerTaskDelegate : NSObject <NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>
  94. @property (nonatomic, weak) AFURLSessionManager *manager;
  95. @property (nonatomic, strong) NSMutableData *mutableData;
  96. @property (nonatomic, strong) NSProgress *uploadProgress;
  97. @property (nonatomic, strong) NSProgress *downloadProgress;
  98. @property (nonatomic, copy) NSURL *downloadFileURL;
  99. @property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading;
  100. @property (nonatomic, copy) AFURLSessionTaskProgressBlock uploadProgressBlock;
  101. @property (nonatomic, copy) AFURLSessionTaskProgressBlock downloadProgressBlock;
  102. @property (nonatomic, copy) AFURLSessionTaskCompletionHandler completionHandler;
  103. @end
  104. @implementation AFURLSessionManagerTaskDelegate
  105. - (instancetype)init {
  106. self = [super init];
  107. if (!self) {
  108. return nil;
  109. }
  110. self.mutableData = [NSMutableData data];
  111. self.uploadProgress = [[NSProgress alloc] initWithParent:nil userInfo:nil];
  112. self.uploadProgress.totalUnitCount = NSURLSessionTransferSizeUnknown;
  113. self.downloadProgress = [[NSProgress alloc] initWithParent:nil userInfo:nil];
  114. self.downloadProgress.totalUnitCount = NSURLSessionTransferSizeUnknown;
  115. return self;
  116. }
  117. #pragma mark - NSProgress Tracking
  118. - (void)setupProgressForTask:(NSURLSessionTask *)task {
  119. __weak __typeof__(task) weakTask = task;
  120. self.uploadProgress.totalUnitCount = task.countOfBytesExpectedToSend;
  121. self.downloadProgress.totalUnitCount = task.countOfBytesExpectedToReceive;
  122. [self.uploadProgress setCancellable:YES];
  123. [self.uploadProgress setCancellationHandler:^{
  124. __typeof__(weakTask) strongTask = weakTask;
  125. [strongTask cancel];
  126. }];
  127. [self.uploadProgress setPausable:YES];
  128. [self.uploadProgress setPausingHandler:^{
  129. __typeof__(weakTask) strongTask = weakTask;
  130. [strongTask suspend];
  131. }];
  132. if ([self.uploadProgress respondsToSelector:@selector(setResumingHandler:)]) {
  133. [self.uploadProgress setResumingHandler:^{
  134. __typeof__(weakTask) strongTask = weakTask;
  135. [strongTask resume];
  136. }];
  137. }
  138. [self.downloadProgress setCancellable:YES];
  139. [self.downloadProgress setCancellationHandler:^{
  140. __typeof__(weakTask) strongTask = weakTask;
  141. [strongTask cancel];
  142. }];
  143. [self.downloadProgress setPausable:YES];
  144. [self.downloadProgress setPausingHandler:^{
  145. __typeof__(weakTask) strongTask = weakTask;
  146. [strongTask suspend];
  147. }];
  148. if ([self.downloadProgress respondsToSelector:@selector(setResumingHandler:)]) {
  149. [self.downloadProgress setResumingHandler:^{
  150. __typeof__(weakTask) strongTask = weakTask;
  151. [strongTask resume];
  152. }];
  153. }
  154. [task addObserver:self
  155. forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))
  156. options:NSKeyValueObservingOptionNew
  157. context:NULL];
  158. [task addObserver:self
  159. forKeyPath:NSStringFromSelector(@selector(countOfBytesExpectedToReceive))
  160. options:NSKeyValueObservingOptionNew
  161. context:NULL];
  162. [task addObserver:self
  163. forKeyPath:NSStringFromSelector(@selector(countOfBytesSent))
  164. options:NSKeyValueObservingOptionNew
  165. context:NULL];
  166. [task addObserver:self
  167. forKeyPath:NSStringFromSelector(@selector(countOfBytesExpectedToSend))
  168. options:NSKeyValueObservingOptionNew
  169. context:NULL];
  170. [self.downloadProgress addObserver:self
  171. forKeyPath:NSStringFromSelector(@selector(fractionCompleted))
  172. options:NSKeyValueObservingOptionNew
  173. context:NULL];
  174. [self.uploadProgress addObserver:self
  175. forKeyPath:NSStringFromSelector(@selector(fractionCompleted))
  176. options:NSKeyValueObservingOptionNew
  177. context:NULL];
  178. }
  179. - (void)cleanUpProgressForTask:(NSURLSessionTask *)task {
  180. [task removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))];
  181. [task removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesExpectedToReceive))];
  182. [task removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesSent))];
  183. [task removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesExpectedToSend))];
  184. [self.downloadProgress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted))];
  185. [self.uploadProgress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted))];
  186. }
  187. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
  188. if ([object isKindOfClass:[NSURLSessionTask class]] || [object isKindOfClass:[NSURLSessionDownloadTask class]]) {
  189. if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) {
  190. self.downloadProgress.completedUnitCount = [change[NSKeyValueChangeNewKey] longLongValue];
  191. } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesExpectedToReceive))]) {
  192. self.downloadProgress.totalUnitCount = [change[NSKeyValueChangeNewKey] longLongValue];
  193. } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesSent))]) {
  194. self.uploadProgress.completedUnitCount = [change[NSKeyValueChangeNewKey] longLongValue];
  195. } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesExpectedToSend))]) {
  196. self.uploadProgress.totalUnitCount = [change[NSKeyValueChangeNewKey] longLongValue];
  197. }
  198. }
  199. else if ([object isEqual:self.downloadProgress]) {
  200. if (self.downloadProgressBlock) {
  201. self.downloadProgressBlock(object);
  202. }
  203. }
  204. else if ([object isEqual:self.uploadProgress]) {
  205. if (self.uploadProgressBlock) {
  206. self.uploadProgressBlock(object);
  207. }
  208. }
  209. }
  210. #pragma mark - NSURLSessionTaskDelegate
  211. - (void)URLSession:(__unused NSURLSession *)session
  212. task:(NSURLSessionTask *)task
  213. didCompleteWithError:(NSError *)error
  214. {
  215. #pragma clang diagnostic push
  216. #pragma clang diagnostic ignored "-Wgnu"
  217. __strong AFURLSessionManager *manager = self.manager;
  218. __block id responseObject = nil;
  219. __block NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  220. userInfo[AFNetworkingTaskDidCompleteResponseSerializerKey] = manager.responseSerializer;
  221. //Performance Improvement from #2672
  222. NSData *data = nil;
  223. if (self.mutableData) {
  224. data = [self.mutableData copy];
  225. //We no longer need the reference, so nil it out to gain back some memory.
  226. self.mutableData = nil;
  227. }
  228. if (self.downloadFileURL) {
  229. userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL;
  230. } else if (data) {
  231. userInfo[AFNetworkingTaskDidCompleteResponseDataKey] = data;
  232. }
  233. if (error) {
  234. userInfo[AFNetworkingTaskDidCompleteErrorKey] = error;
  235. dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
  236. if (self.completionHandler) {
  237. self.completionHandler(task.response, responseObject, error);
  238. }
  239. dispatch_async(dispatch_get_main_queue(), ^{
  240. [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
  241. });
  242. });
  243. } else {
  244. dispatch_async(url_session_manager_processing_queue(), ^{
  245. NSError *serializationError = nil;
  246. responseObject = [manager.responseSerializer responseObjectForResponse:task.response data:data error:&serializationError];
  247. if (self.downloadFileURL) {
  248. responseObject = self.downloadFileURL;
  249. }
  250. if (responseObject) {
  251. userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey] = responseObject;
  252. }
  253. if (serializationError) {
  254. userInfo[AFNetworkingTaskDidCompleteErrorKey] = serializationError;
  255. }
  256. dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
  257. if (self.completionHandler) {
  258. self.completionHandler(task.response, responseObject, serializationError);
  259. }
  260. dispatch_async(dispatch_get_main_queue(), ^{
  261. [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
  262. });
  263. });
  264. });
  265. }
  266. #pragma clang diagnostic pop
  267. }
  268. #pragma mark - NSURLSessionDataTaskDelegate
  269. - (void)URLSession:(__unused NSURLSession *)session
  270. dataTask:(__unused NSURLSessionDataTask *)dataTask
  271. didReceiveData:(NSData *)data
  272. {
  273. [self.mutableData appendData:data];
  274. }
  275. #pragma mark - NSURLSessionDownloadTaskDelegate
  276. - (void)URLSession:(NSURLSession *)session
  277. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  278. didFinishDownloadingToURL:(NSURL *)location
  279. {
  280. NSError *fileManagerError = nil;
  281. self.downloadFileURL = nil;
  282. if (self.downloadTaskDidFinishDownloading) {
  283. self.downloadFileURL = self.downloadTaskDidFinishDownloading(session, downloadTask, location);
  284. if (self.downloadFileURL) {
  285. [[NSFileManager defaultManager] removeItemAtPath:self.downloadFileURL.path error:nil];
  286. [[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError];
  287. if (fileManagerError) {
  288. [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo];
  289. }
  290. }
  291. }
  292. }
  293. @end
  294. #pragma mark -
  295. /**
  296. * A workaround for issues related to key-value observing the `state` of an `NSURLSessionTask`.
  297. *
  298. * See:
  299. * - https://github.com/AFNetworking/AFNetworking/issues/1477
  300. * - https://github.com/AFNetworking/AFNetworking/issues/2638
  301. * - https://github.com/AFNetworking/AFNetworking/pull/2702
  302. */
  303. static inline void af_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector) {
  304. Method originalMethod = class_getInstanceMethod(theClass, originalSelector);
  305. Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);
  306. method_exchangeImplementations(originalMethod, swizzledMethod);
  307. }
  308. static inline BOOL af_addMethod(Class theClass, SEL selector, Method method) {
  309. return class_addMethod(theClass, selector, method_getImplementation(method), method_getTypeEncoding(method));
  310. }
  311. static NSString * const AFNSURLSessionTaskDidResumeNotification = @"com.alamofire.networking.nsurlsessiontask.resume";
  312. static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofire.networking.nsurlsessiontask.suspend";
  313. @interface _AFURLSessionTaskSwizzling : NSObject
  314. @end
  315. @implementation _AFURLSessionTaskSwizzling
  316. + (void)load {
  317. /**
  318. WARNING: Trouble Ahead
  319. https://github.com/AFNetworking/AFNetworking/pull/2702
  320. */
  321. if (NSClassFromString(@"NSURLSessionTask")) {
  322. /**
  323. iOS 7 and iOS 8 differ in NSURLSessionTask implementation, which makes the next bit of code a bit tricky.
  324. Many Unit Tests have been built to validate as much of this behavior has possible.
  325. Here is what we know:
  326. - NSURLSessionTasks are implemented with class clusters, meaning the class you request from the API isn't actually the type of class you will get back.
  327. - Simply referencing `[NSURLSessionTask class]` will not work. You need to ask an `NSURLSession` to actually create an object, and grab the class from there.
  328. - On iOS 7, `localDataTask` is a `__NSCFLocalDataTask`, which inherits from `__NSCFLocalSessionTask`, which inherits from `__NSCFURLSessionTask`.
  329. - On iOS 8, `localDataTask` is a `__NSCFLocalDataTask`, which inherits from `__NSCFLocalSessionTask`, which inherits from `NSURLSessionTask`.
  330. - On iOS 7, `__NSCFLocalSessionTask` and `__NSCFURLSessionTask` are the only two classes that have their own implementations of `resume` and `suspend`, and `__NSCFLocalSessionTask` DOES NOT CALL SUPER. This means both classes need to be swizzled.
  331. - On iOS 8, `NSURLSessionTask` is the only class that implements `resume` and `suspend`. This means this is the only class that needs to be swizzled.
  332. - Because `NSURLSessionTask` is not involved in the class hierarchy for every version of iOS, its easier to add the swizzled methods to a dummy class and manage them there.
  333. Some Assumptions:
  334. - No implementations of `resume` or `suspend` call super. If this were to change in a future version of iOS, we'd need to handle it.
  335. - No background task classes override `resume` or `suspend`
  336. The current solution:
  337. 1) Grab an instance of `__NSCFLocalDataTask` by asking an instance of `NSURLSession` for a data task.
  338. 2) Grab a pointer to the original implementation of `af_resume`
  339. 3) Check to see if the current class has an implementation of resume. If so, continue to step 4.
  340. 4) Grab the super class of the current class.
  341. 5) Grab a pointer for the current class to the current implementation of `resume`.
  342. 6) Grab a pointer for the super class to the current implementation of `resume`.
  343. 7) If the current class implementation of `resume` is not equal to the super class implementation of `resume` AND the current implementation of `resume` is not equal to the original implementation of `af_resume`, THEN swizzle the methods
  344. 8) Set the current class to the super class, and repeat steps 3-8
  345. */
  346. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
  347. NSURLSession * session = [NSURLSession sessionWithConfiguration:configuration];
  348. #pragma GCC diagnostic push
  349. #pragma GCC diagnostic ignored "-Wnonnull"
  350. NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil];
  351. #pragma clang diagnostic pop
  352. IMP originalAFResumeIMP = method_getImplementation(class_getInstanceMethod([self class], @selector(af_resume)));
  353. Class currentClass = [localDataTask class];
  354. while (class_getInstanceMethod(currentClass, @selector(resume))) {
  355. Class superClass = [currentClass superclass];
  356. IMP classResumeIMP = method_getImplementation(class_getInstanceMethod(currentClass, @selector(resume)));
  357. IMP superclassResumeIMP = method_getImplementation(class_getInstanceMethod(superClass, @selector(resume)));
  358. if (classResumeIMP != superclassResumeIMP &&
  359. originalAFResumeIMP != classResumeIMP) {
  360. [self swizzleResumeAndSuspendMethodForClass:currentClass];
  361. }
  362. currentClass = [currentClass superclass];
  363. }
  364. [localDataTask cancel];
  365. [session finishTasksAndInvalidate];
  366. }
  367. }
  368. + (void)swizzleResumeAndSuspendMethodForClass:(Class)theClass {
  369. Method afResumeMethod = class_getInstanceMethod(self, @selector(af_resume));
  370. Method afSuspendMethod = class_getInstanceMethod(self, @selector(af_suspend));
  371. if (af_addMethod(theClass, @selector(af_resume), afResumeMethod)) {
  372. af_swizzleSelector(theClass, @selector(resume), @selector(af_resume));
  373. }
  374. if (af_addMethod(theClass, @selector(af_suspend), afSuspendMethod)) {
  375. af_swizzleSelector(theClass, @selector(suspend), @selector(af_suspend));
  376. }
  377. }
  378. - (NSURLSessionTaskState)state {
  379. NSAssert(NO, @"State method should never be called in the actual dummy class");
  380. return NSURLSessionTaskStateCanceling;
  381. }
  382. - (void)af_resume {
  383. NSAssert([self respondsToSelector:@selector(state)], @"Does not respond to state");
  384. NSURLSessionTaskState state = [self state];
  385. [self af_resume];
  386. if (state != NSURLSessionTaskStateRunning) {
  387. [[NSNotificationCenter defaultCenter] postNotificationName:AFNSURLSessionTaskDidResumeNotification object:self];
  388. }
  389. }
  390. - (void)af_suspend {
  391. NSAssert([self respondsToSelector:@selector(state)], @"Does not respond to state");
  392. NSURLSessionTaskState state = [self state];
  393. [self af_suspend];
  394. if (state != NSURLSessionTaskStateSuspended) {
  395. [[NSNotificationCenter defaultCenter] postNotificationName:AFNSURLSessionTaskDidSuspendNotification object:self];
  396. }
  397. }
  398. @end
  399. #pragma mark -
  400. @interface AFURLSessionManager ()
  401. @property (readwrite, nonatomic, strong) NSURLSessionConfiguration *sessionConfiguration;
  402. @property (readwrite, nonatomic, strong) NSOperationQueue *operationQueue;
  403. @property (readwrite, nonatomic, strong) NSURLSession *session;
  404. @property (readwrite, nonatomic, strong) NSMutableDictionary *mutableTaskDelegatesKeyedByTaskIdentifier;
  405. @property (readonly, nonatomic, copy) NSString *taskDescriptionForSessionTasks;
  406. @property (readwrite, nonatomic, strong) NSLock *lock;
  407. @property (readwrite, nonatomic, copy) AFURLSessionDidBecomeInvalidBlock sessionDidBecomeInvalid;
  408. @property (readwrite, nonatomic, copy) AFURLSessionDidReceiveAuthenticationChallengeBlock sessionDidReceiveAuthenticationChallenge;
  409. @property (readwrite, nonatomic, copy) AFURLSessionDidFinishEventsForBackgroundURLSessionBlock didFinishEventsForBackgroundURLSession;
  410. @property (readwrite, nonatomic, copy) AFURLSessionTaskWillPerformHTTPRedirectionBlock taskWillPerformHTTPRedirection;
  411. @property (readwrite, nonatomic, copy) AFURLSessionTaskDidReceiveAuthenticationChallengeBlock taskDidReceiveAuthenticationChallenge;
  412. @property (readwrite, nonatomic, copy) AFURLSessionTaskNeedNewBodyStreamBlock taskNeedNewBodyStream;
  413. @property (readwrite, nonatomic, copy) AFURLSessionTaskDidSendBodyDataBlock taskDidSendBodyData;
  414. @property (readwrite, nonatomic, copy) AFURLSessionTaskDidCompleteBlock taskDidComplete;
  415. @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidReceiveResponseBlock dataTaskDidReceiveResponse;
  416. @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidBecomeDownloadTaskBlock dataTaskDidBecomeDownloadTask;
  417. @property (readwrite, nonatomic, copy) AFURLSessionDataTaskDidReceiveDataBlock dataTaskDidReceiveData;
  418. @property (readwrite, nonatomic, copy) AFURLSessionDataTaskWillCacheResponseBlock dataTaskWillCacheResponse;
  419. @property (readwrite, nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading;
  420. @property (readwrite, nonatomic, copy) AFURLSessionDownloadTaskDidWriteDataBlock downloadTaskDidWriteData;
  421. @property (readwrite, nonatomic, copy) AFURLSessionDownloadTaskDidResumeBlock downloadTaskDidResume;
  422. @end
  423. @implementation AFURLSessionManager
  424. - (instancetype)init {
  425. return [self initWithSessionConfiguration:nil];
  426. }
  427. - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
  428. self = [super init];
  429. if (!self) {
  430. return nil;
  431. }
  432. if (!configuration) {
  433. configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  434. }
  435. self.sessionConfiguration = configuration;
  436. self.operationQueue = [[NSOperationQueue alloc] init];
  437. self.operationQueue.maxConcurrentOperationCount = 1;
  438. self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
  439. self.responseSerializer = [AFJSONResponseSerializer serializer];
  440. self.securityPolicy = [AFSecurityPolicy defaultPolicy];
  441. #if !TARGET_OS_WATCH
  442. self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
  443. #endif
  444. self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];
  445. self.lock = [[NSLock alloc] init];
  446. self.lock.name = AFURLSessionManagerLockName;
  447. [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
  448. for (NSURLSessionDataTask *task in dataTasks) {
  449. [self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
  450. }
  451. for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
  452. [self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
  453. }
  454. for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
  455. [self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
  456. }
  457. }];
  458. return self;
  459. }
  460. - (void)dealloc {
  461. [[NSNotificationCenter defaultCenter] removeObserver:self];
  462. }
  463. #pragma mark -
  464. - (NSString *)taskDescriptionForSessionTasks {
  465. return [NSString stringWithFormat:@"%p", self];
  466. }
  467. - (void)taskDidResume:(NSNotification *)notification {
  468. NSURLSessionTask *task = notification.object;
  469. if ([task respondsToSelector:@selector(taskDescription)]) {
  470. if ([task.taskDescription isEqualToString:self.taskDescriptionForSessionTasks]) {
  471. dispatch_async(dispatch_get_main_queue(), ^{
  472. [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidResumeNotification object:task];
  473. });
  474. }
  475. }
  476. }
  477. - (void)taskDidSuspend:(NSNotification *)notification {
  478. NSURLSessionTask *task = notification.object;
  479. if ([task respondsToSelector:@selector(taskDescription)]) {
  480. if ([task.taskDescription isEqualToString:self.taskDescriptionForSessionTasks]) {
  481. dispatch_async(dispatch_get_main_queue(), ^{
  482. [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidSuspendNotification object:task];
  483. });
  484. }
  485. }
  486. }
  487. #pragma mark -
  488. - (AFURLSessionManagerTaskDelegate *)delegateForTask:(NSURLSessionTask *)task {
  489. NSParameterAssert(task);
  490. AFURLSessionManagerTaskDelegate *delegate = nil;
  491. [self.lock lock];
  492. delegate = self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)];
  493. [self.lock unlock];
  494. return delegate;
  495. }
  496. - (void)setDelegate:(AFURLSessionManagerTaskDelegate *)delegate
  497. forTask:(NSURLSessionTask *)task
  498. {
  499. NSParameterAssert(task);
  500. NSParameterAssert(delegate);
  501. [self.lock lock];
  502. self.mutableTaskDelegatesKeyedByTaskIdentifier[@(task.taskIdentifier)] = delegate;
  503. [delegate setupProgressForTask:task];
  504. [self addNotificationObserverForTask:task];
  505. [self.lock unlock];
  506. }
  507. - (void)addDelegateForDataTask:(NSURLSessionDataTask *)dataTask
  508. uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
  509. downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
  510. completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
  511. {
  512. AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init];
  513. delegate.manager = self;
  514. delegate.completionHandler = completionHandler;
  515. dataTask.taskDescription = self.taskDescriptionForSessionTasks;
  516. [self setDelegate:delegate forTask:dataTask];
  517. delegate.uploadProgressBlock = uploadProgressBlock;
  518. delegate.downloadProgressBlock = downloadProgressBlock;
  519. }
  520. - (void)addDelegateForUploadTask:(NSURLSessionUploadTask *)uploadTask
  521. progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
  522. completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
  523. {
  524. AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init];
  525. delegate.manager = self;
  526. delegate.completionHandler = completionHandler;
  527. uploadTask.taskDescription = self.taskDescriptionForSessionTasks;
  528. [self setDelegate:delegate forTask:uploadTask];
  529. delegate.uploadProgressBlock = uploadProgressBlock;
  530. }
  531. - (void)addDelegateForDownloadTask:(NSURLSessionDownloadTask *)downloadTask
  532. progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock
  533. destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
  534. completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
  535. {
  536. AFURLSessionManagerTaskDelegate *delegate = [[AFURLSessionManagerTaskDelegate alloc] init];
  537. delegate.manager = self;
  538. delegate.completionHandler = completionHandler;
  539. if (destination) {
  540. delegate.downloadTaskDidFinishDownloading = ^NSURL * (NSURLSession * __unused session, NSURLSessionDownloadTask *task, NSURL *location) {
  541. return destination(location, task.response);
  542. };
  543. }
  544. downloadTask.taskDescription = self.taskDescriptionForSessionTasks;
  545. [self setDelegate:delegate forTask:downloadTask];
  546. delegate.downloadProgressBlock = downloadProgressBlock;
  547. }
  548. - (void)removeDelegateForTask:(NSURLSessionTask *)task {
  549. NSParameterAssert(task);
  550. AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task];
  551. [self.lock lock];
  552. [delegate cleanUpProgressForTask:task];
  553. [self removeNotificationObserverForTask:task];
  554. [self.mutableTaskDelegatesKeyedByTaskIdentifier removeObjectForKey:@(task.taskIdentifier)];
  555. [self.lock unlock];
  556. }
  557. #pragma mark -
  558. - (NSArray *)tasksForKeyPath:(NSString *)keyPath {
  559. __block NSArray *tasks = nil;
  560. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  561. [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
  562. if ([keyPath isEqualToString:NSStringFromSelector(@selector(dataTasks))]) {
  563. tasks = dataTasks;
  564. } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(uploadTasks))]) {
  565. tasks = uploadTasks;
  566. } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(downloadTasks))]) {
  567. tasks = downloadTasks;
  568. } else if ([keyPath isEqualToString:NSStringFromSelector(@selector(tasks))]) {
  569. tasks = [@[dataTasks, uploadTasks, downloadTasks] valueForKeyPath:@"@unionOfArrays.self"];
  570. }
  571. dispatch_semaphore_signal(semaphore);
  572. }];
  573. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  574. return tasks;
  575. }
  576. - (NSArray *)tasks {
  577. return [self tasksForKeyPath:NSStringFromSelector(_cmd)];
  578. }
  579. - (NSArray *)dataTasks {
  580. return [self tasksForKeyPath:NSStringFromSelector(_cmd)];
  581. }
  582. - (NSArray *)uploadTasks {
  583. return [self tasksForKeyPath:NSStringFromSelector(_cmd)];
  584. }
  585. - (NSArray *)downloadTasks {
  586. return [self tasksForKeyPath:NSStringFromSelector(_cmd)];
  587. }
  588. #pragma mark -
  589. - (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks {
  590. dispatch_async(dispatch_get_main_queue(), ^{
  591. if (cancelPendingTasks) {
  592. [self.session invalidateAndCancel];
  593. } else {
  594. [self.session finishTasksAndInvalidate];
  595. }
  596. });
  597. }
  598. #pragma mark -
  599. - (void)setResponseSerializer:(id <AFURLResponseSerialization>)responseSerializer {
  600. NSParameterAssert(responseSerializer);
  601. _responseSerializer = responseSerializer;
  602. }
  603. #pragma mark -
  604. - (void)addNotificationObserverForTask:(NSURLSessionTask *)task {
  605. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidResume:) name:AFNSURLSessionTaskDidResumeNotification object:task];
  606. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidSuspend:) name:AFNSURLSessionTaskDidSuspendNotification object:task];
  607. }
  608. - (void)removeNotificationObserverForTask:(NSURLSessionTask *)task {
  609. [[NSNotificationCenter defaultCenter] removeObserver:self name:AFNSURLSessionTaskDidSuspendNotification object:task];
  610. [[NSNotificationCenter defaultCenter] removeObserver:self name:AFNSURLSessionTaskDidResumeNotification object:task];
  611. }
  612. #pragma mark -
  613. - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
  614. completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
  615. {
  616. return [self dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:completionHandler];
  617. }
  618. - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
  619. uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgressBlock
  620. downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgressBlock
  621. completionHandler:(nullable void (^)(NSURLResponse *response, id _Nullable responseObject, NSError * _Nullable error))completionHandler {
  622. __block NSURLSessionDataTask *dataTask = nil;
  623. url_session_manager_create_task_safely(^{
  624. dataTask = [self.session dataTaskWithRequest:request];
  625. });
  626. [self addDelegateForDataTask:dataTask uploadProgress:uploadProgressBlock downloadProgress:downloadProgressBlock completionHandler:completionHandler];
  627. return dataTask;
  628. }
  629. #pragma mark -
  630. - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
  631. fromFile:(NSURL *)fileURL
  632. progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
  633. completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
  634. {
  635. __block NSURLSessionUploadTask *uploadTask = nil;
  636. url_session_manager_create_task_safely(^{
  637. uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
  638. });
  639. if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) {
  640. for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) {
  641. uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
  642. }
  643. }
  644. [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler];
  645. return uploadTask;
  646. }
  647. - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
  648. fromData:(NSData *)bodyData
  649. progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
  650. completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
  651. {
  652. __block NSURLSessionUploadTask *uploadTask = nil;
  653. url_session_manager_create_task_safely(^{
  654. uploadTask = [self.session uploadTaskWithRequest:request fromData:bodyData];
  655. });
  656. [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler];
  657. return uploadTask;
  658. }
  659. - (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request
  660. progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
  661. completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
  662. {
  663. __block NSURLSessionUploadTask *uploadTask = nil;
  664. url_session_manager_create_task_safely(^{
  665. uploadTask = [self.session uploadTaskWithStreamedRequest:request];
  666. });
  667. [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler];
  668. return uploadTask;
  669. }
  670. #pragma mark -
  671. - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
  672. progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock
  673. destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
  674. completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
  675. {
  676. __block NSURLSessionDownloadTask *downloadTask = nil;
  677. url_session_manager_create_task_safely(^{
  678. downloadTask = [self.session downloadTaskWithRequest:request];
  679. });
  680. [self addDelegateForDownloadTask:downloadTask progress:downloadProgressBlock destination:destination completionHandler:completionHandler];
  681. return downloadTask;
  682. }
  683. - (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData
  684. progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock
  685. destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination
  686. completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
  687. {
  688. __block NSURLSessionDownloadTask *downloadTask = nil;
  689. url_session_manager_create_task_safely(^{
  690. downloadTask = [self.session downloadTaskWithResumeData:resumeData];
  691. });
  692. [self addDelegateForDownloadTask:downloadTask progress:downloadProgressBlock destination:destination completionHandler:completionHandler];
  693. return downloadTask;
  694. }
  695. #pragma mark -
  696. - (NSProgress *)uploadProgressForTask:(NSURLSessionTask *)task {
  697. return [[self delegateForTask:task] uploadProgress];
  698. }
  699. - (NSProgress *)downloadProgressForTask:(NSURLSessionTask *)task {
  700. return [[self delegateForTask:task] downloadProgress];
  701. }
  702. #pragma mark -
  703. - (void)setSessionDidBecomeInvalidBlock:(void (^)(NSURLSession *session, NSError *error))block {
  704. self.sessionDidBecomeInvalid = block;
  705. }
  706. - (void)setSessionDidReceiveAuthenticationChallengeBlock:(NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential))block {
  707. self.sessionDidReceiveAuthenticationChallenge = block;
  708. }
  709. - (void)setDidFinishEventsForBackgroundURLSessionBlock:(void (^)(NSURLSession *session))block {
  710. self.didFinishEventsForBackgroundURLSession = block;
  711. }
  712. #pragma mark -
  713. - (void)setTaskNeedNewBodyStreamBlock:(NSInputStream * (^)(NSURLSession *session, NSURLSessionTask *task))block {
  714. self.taskNeedNewBodyStream = block;
  715. }
  716. - (void)setTaskWillPerformHTTPRedirectionBlock:(NSURLRequest * (^)(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request))block {
  717. self.taskWillPerformHTTPRedirection = block;
  718. }
  719. - (void)setTaskDidReceiveAuthenticationChallengeBlock:(NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLSessionTask *task, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential))block {
  720. self.taskDidReceiveAuthenticationChallenge = block;
  721. }
  722. - (void)setTaskDidSendBodyDataBlock:(void (^)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend))block {
  723. self.taskDidSendBodyData = block;
  724. }
  725. - (void)setTaskDidCompleteBlock:(void (^)(NSURLSession *session, NSURLSessionTask *task, NSError *error))block {
  726. self.taskDidComplete = block;
  727. }
  728. #pragma mark -
  729. - (void)setDataTaskDidReceiveResponseBlock:(NSURLSessionResponseDisposition (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response))block {
  730. self.dataTaskDidReceiveResponse = block;
  731. }
  732. - (void)setDataTaskDidBecomeDownloadTaskBlock:(void (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask))block {
  733. self.dataTaskDidBecomeDownloadTask = block;
  734. }
  735. - (void)setDataTaskDidReceiveDataBlock:(void (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data))block {
  736. self.dataTaskDidReceiveData = block;
  737. }
  738. - (void)setDataTaskWillCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse))block {
  739. self.dataTaskWillCacheResponse = block;
  740. }
  741. #pragma mark -
  742. - (void)setDownloadTaskDidFinishDownloadingBlock:(NSURL * (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block {
  743. self.downloadTaskDidFinishDownloading = block;
  744. }
  745. - (void)setDownloadTaskDidWriteDataBlock:(void (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite))block {
  746. self.downloadTaskDidWriteData = block;
  747. }
  748. - (void)setDownloadTaskDidResumeBlock:(void (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t fileOffset, int64_t expectedTotalBytes))block {
  749. self.downloadTaskDidResume = block;
  750. }
  751. #pragma mark - NSObject
  752. - (NSString *)description {
  753. return [NSString stringWithFormat:@"<%@: %p, session: %@, operationQueue: %@>", NSStringFromClass([self class]), self, self.session, self.operationQueue];
  754. }
  755. - (BOOL)respondsToSelector:(SEL)selector {
  756. if (selector == @selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:)) {
  757. return self.taskWillPerformHTTPRedirection != nil;
  758. } else if (selector == @selector(URLSession:dataTask:didReceiveResponse:completionHandler:)) {
  759. return self.dataTaskDidReceiveResponse != nil;
  760. } else if (selector == @selector(URLSession:dataTask:willCacheResponse:completionHandler:)) {
  761. return self.dataTaskWillCacheResponse != nil;
  762. } else if (selector == @selector(URLSessionDidFinishEventsForBackgroundURLSession:)) {
  763. return self.didFinishEventsForBackgroundURLSession != nil;
  764. }
  765. return [[self class] instancesRespondToSelector:selector];
  766. }
  767. #pragma mark - NSURLSessionDelegate
  768. - (void)URLSession:(NSURLSession *)session
  769. didBecomeInvalidWithError:(NSError *)error
  770. {
  771. if (self.sessionDidBecomeInvalid) {
  772. self.sessionDidBecomeInvalid(session, error);
  773. }
  774. [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDidInvalidateNotification object:session];
  775. }
  776. - (void)URLSession:(NSURLSession *)session
  777. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  778. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  779. {
  780. NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  781. __block NSURLCredential *credential = nil;
  782. if (self.sessionDidReceiveAuthenticationChallenge) {
  783. disposition = self.sessionDidReceiveAuthenticationChallenge(session, challenge, &credential);
  784. } else {
  785. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
  786. if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
  787. credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
  788. if (credential) {
  789. disposition = NSURLSessionAuthChallengeUseCredential;
  790. } else {
  791. disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  792. }
  793. } else {
  794. disposition = NSURLSessionAuthChallengeRejectProtectionSpace;
  795. }
  796. } else {
  797. disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  798. }
  799. }
  800. if (completionHandler) {
  801. completionHandler(disposition, credential);
  802. }
  803. }
  804. #pragma mark - NSURLSessionTaskDelegate
  805. - (void)URLSession:(NSURLSession *)session
  806. task:(NSURLSessionTask *)task
  807. willPerformHTTPRedirection:(NSHTTPURLResponse *)response
  808. newRequest:(NSURLRequest *)request
  809. completionHandler:(void (^)(NSURLRequest *))completionHandler
  810. {
  811. NSURLRequest *redirectRequest = request;
  812. if (self.taskWillPerformHTTPRedirection) {
  813. redirectRequest = self.taskWillPerformHTTPRedirection(session, task, response, request);
  814. }
  815. if (completionHandler) {
  816. completionHandler(redirectRequest);
  817. }
  818. }
  819. - (void)URLSession:(NSURLSession *)session
  820. task:(NSURLSessionTask *)task
  821. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  822. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  823. {
  824. NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  825. __block NSURLCredential *credential = nil;
  826. if (self.taskDidReceiveAuthenticationChallenge) {
  827. disposition = self.taskDidReceiveAuthenticationChallenge(session, task, challenge, &credential);
  828. } else {
  829. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
  830. if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
  831. disposition = NSURLSessionAuthChallengeUseCredential;
  832. credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
  833. } else {
  834. disposition = NSURLSessionAuthChallengeRejectProtectionSpace;
  835. }
  836. } else {
  837. disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  838. }
  839. }
  840. if (completionHandler) {
  841. completionHandler(disposition, credential);
  842. }
  843. }
  844. - (void)URLSession:(NSURLSession *)session
  845. task:(NSURLSessionTask *)task
  846. needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler
  847. {
  848. NSInputStream *inputStream = nil;
  849. if (self.taskNeedNewBodyStream) {
  850. inputStream = self.taskNeedNewBodyStream(session, task);
  851. } else if (task.originalRequest.HTTPBodyStream && [task.originalRequest.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) {
  852. inputStream = [task.originalRequest.HTTPBodyStream copy];
  853. }
  854. if (completionHandler) {
  855. completionHandler(inputStream);
  856. }
  857. }
  858. - (void)URLSession:(NSURLSession *)session
  859. task:(NSURLSessionTask *)task
  860. didSendBodyData:(int64_t)bytesSent
  861. totalBytesSent:(int64_t)totalBytesSent
  862. totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
  863. {
  864. int64_t totalUnitCount = totalBytesExpectedToSend;
  865. if(totalUnitCount == NSURLSessionTransferSizeUnknown) {
  866. NSString *contentLength = [task.originalRequest valueForHTTPHeaderField:@"Content-Length"];
  867. if(contentLength) {
  868. totalUnitCount = (int64_t) [contentLength longLongValue];
  869. }
  870. }
  871. if (self.taskDidSendBodyData) {
  872. self.taskDidSendBodyData(session, task, bytesSent, totalBytesSent, totalUnitCount);
  873. }
  874. }
  875. - (void)URLSession:(NSURLSession *)session
  876. task:(NSURLSessionTask *)task
  877. didCompleteWithError:(NSError *)error
  878. {
  879. AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task];
  880. // delegate may be nil when completing a task in the background
  881. if (delegate) {
  882. [delegate URLSession:session task:task didCompleteWithError:error];
  883. [self removeDelegateForTask:task];
  884. }
  885. if (self.taskDidComplete) {
  886. self.taskDidComplete(session, task, error);
  887. }
  888. }
  889. #pragma mark - NSURLSessionDataDelegate
  890. - (void)URLSession:(NSURLSession *)session
  891. dataTask:(NSURLSessionDataTask *)dataTask
  892. didReceiveResponse:(NSURLResponse *)response
  893. completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
  894. {
  895. NSURLSessionResponseDisposition disposition = NSURLSessionResponseAllow;
  896. if (self.dataTaskDidReceiveResponse) {
  897. disposition = self.dataTaskDidReceiveResponse(session, dataTask, response);
  898. }
  899. if (completionHandler) {
  900. completionHandler(disposition);
  901. }
  902. }
  903. - (void)URLSession:(NSURLSession *)session
  904. dataTask:(NSURLSessionDataTask *)dataTask
  905. didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask
  906. {
  907. AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:dataTask];
  908. if (delegate) {
  909. [self removeDelegateForTask:dataTask];
  910. [self setDelegate:delegate forTask:downloadTask];
  911. }
  912. if (self.dataTaskDidBecomeDownloadTask) {
  913. self.dataTaskDidBecomeDownloadTask(session, dataTask, downloadTask);
  914. }
  915. }
  916. - (void)URLSession:(NSURLSession *)session
  917. dataTask:(NSURLSessionDataTask *)dataTask
  918. didReceiveData:(NSData *)data
  919. {
  920. AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:dataTask];
  921. [delegate URLSession:session dataTask:dataTask didReceiveData:data];
  922. if (self.dataTaskDidReceiveData) {
  923. self.dataTaskDidReceiveData(session, dataTask, data);
  924. }
  925. }
  926. - (void)URLSession:(NSURLSession *)session
  927. dataTask:(NSURLSessionDataTask *)dataTask
  928. willCacheResponse:(NSCachedURLResponse *)proposedResponse
  929. completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler
  930. {
  931. NSCachedURLResponse *cachedResponse = proposedResponse;
  932. if (self.dataTaskWillCacheResponse) {
  933. cachedResponse = self.dataTaskWillCacheResponse(session, dataTask, proposedResponse);
  934. }
  935. if (completionHandler) {
  936. completionHandler(cachedResponse);
  937. }
  938. }
  939. - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
  940. if (self.didFinishEventsForBackgroundURLSession) {
  941. dispatch_async(dispatch_get_main_queue(), ^{
  942. self.didFinishEventsForBackgroundURLSession(session);
  943. });
  944. }
  945. }
  946. #pragma mark - NSURLSessionDownloadDelegate
  947. - (void)URLSession:(NSURLSession *)session
  948. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  949. didFinishDownloadingToURL:(NSURL *)location
  950. {
  951. AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:downloadTask];
  952. if (self.downloadTaskDidFinishDownloading) {
  953. NSURL *fileURL = self.downloadTaskDidFinishDownloading(session, downloadTask, location);
  954. if (fileURL) {
  955. delegate.downloadFileURL = fileURL;
  956. NSError *error = nil;
  957. [[NSFileManager defaultManager] removeItemAtPath:fileURL.path error:nil];
  958. [[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error];
  959. if (error) {
  960. [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:error.userInfo];
  961. }
  962. return;
  963. }
  964. }
  965. if (delegate) {
  966. [delegate URLSession:session downloadTask:downloadTask didFinishDownloadingToURL:location];
  967. }
  968. }
  969. - (void)URLSession:(NSURLSession *)session
  970. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  971. didWriteData:(int64_t)bytesWritten
  972. totalBytesWritten:(int64_t)totalBytesWritten
  973. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
  974. {
  975. if (self.downloadTaskDidWriteData) {
  976. self.downloadTaskDidWriteData(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  977. }
  978. }
  979. - (void)URLSession:(NSURLSession *)session
  980. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  981. didResumeAtOffset:(int64_t)fileOffset
  982. expectedTotalBytes:(int64_t)expectedTotalBytes
  983. {
  984. if (self.downloadTaskDidResume) {
  985. self.downloadTaskDidResume(session, downloadTask, fileOffset, expectedTotalBytes);
  986. }
  987. }
  988. #pragma mark - NSSecureCoding
  989. + (BOOL)supportsSecureCoding {
  990. return YES;
  991. }
  992. - (instancetype)initWithCoder:(NSCoder *)decoder {
  993. NSURLSessionConfiguration *configuration = [decoder decodeObjectOfClass:[NSURLSessionConfiguration class] forKey:@"sessionConfiguration"];
  994. self = [self initWithSessionConfiguration:configuration];
  995. if (!self) {
  996. return nil;
  997. }
  998. return self;
  999. }
  1000. - (void)encodeWithCoder:(NSCoder *)coder {
  1001. [coder encodeObject:self.session.configuration forKey:@"sessionConfiguration"];
  1002. }
  1003. #pragma mark - NSCopying
  1004. - (instancetype)copyWithZone:(NSZone *)zone {
  1005. return [[[self class] allocWithZone:zone] initWithSessionConfiguration:self.session.configuration];
  1006. }
  1007. @end