mixDownloadSession.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // mixDownloadSession.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/6/27.
  6. //
  7. #import "mixDownloadSession.h"
  8. #import "mixDownloadQueue.h"
  9. #import "mixDownloadOperation.h"
  10. @interface mixDownloadSession () <NSURLSessionDataDelegate>
  11. /** session 可以支持多个任务下载,创建一次就可以 */
  12. @property (nonatomic,strong) NSURLSession *session;
  13. /** 下载列队管理 专门负责接收到数据时分配给不同operation */
  14. @property (nonatomic,strong) mixDownloadQueue *queue;
  15. @end
  16. @implementation mixDownloadSession
  17. // 添加任务
  18. - (void)downloadWithURL:(NSURL *)url begin:(void(^)(NSString *))begin progress:(void(^)(NSInteger,NSInteger))progress complete:(void(^)(NSDictionary *,NSError *))complet {
  19. // 交给列队管理
  20. [self.queue addDownloadWithSession:self.session URL:url begin:begin progress:progress complete:complet];
  21. }
  22. #pragma mark - 操作任务接口
  23. - (void)startDownLoadWithUrl:(NSString *)url {
  24. [self.queue operateDownloadWithUrl:url session:self.session handle:DownloadHandleTypeStart];
  25. }
  26. - (void)supendDownloadWithUrl:(NSString *)url {
  27. [self.queue operateDownloadWithUrl:url session:self.session handle:DownloadHandleTypeSuspend];
  28. }
  29. - (void)cancelDownloadWithUrl:(NSString *)url {
  30. [self.queue operateDownloadWithUrl:url session:self.session handle:DownloadHandleTypeCancel];
  31. }
  32. - (void)cancelAllDownloads {
  33. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  34. // 取消所有session的任务
  35. // 耗时操作
  36. // 会调用 URLSession:task:didCompleteWithError: 方法抛出error取消
  37. [self.session invalidateAndCancel];
  38. //删除本地新鲜
  39. [mixDownloadCacheManager clearMemoryAndDisk];
  40. });
  41. }
  42. - (void)startAllDownloads {
  43. [self.queue startAllTasksWithSession:self.session];
  44. }
  45. - (void)suspendAllDownloads {
  46. [self.queue suspendAllTasksWithSession:self.session];
  47. }
  48. #pragma mark - <NSURLSessionDataDelegate>
  49. // ssl 服务 证书信任
  50. - (void)URLSession:(NSURLSession *)session
  51. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  52. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
  53. if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
  54. return;
  55. }
  56. // 信任该插件
  57. NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  58. // 第一个参数 告诉系统如何处置
  59. completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
  60. }
  61. //当请求协议是https的时候回调用该方法
  62. //Challenge 挑战 质询(受保护空间)
  63. //NSURLAuthenticationMethodServerTrust 服务器信任证书
  64. - (void)URLSession:(NSURLSession *)session
  65. task:(NSURLSessionTask *)task
  66. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  67. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
  68. if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
  69. return;
  70. }
  71. // 信任该插件
  72. NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  73. // 第一个参数 告诉系统如何处置
  74. completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
  75. }
  76. // 接受到响应调用
  77. - (void)URLSession:(NSURLSession *)session
  78. dataTask:(NSURLSessionDataTask *)dataTask
  79. didReceiveResponse:(NSURLResponse *)response
  80. completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
  81. // 将响应交给列队处理
  82. [self.queue dataTask:dataTask didReceiveResponse:response];
  83. // 允许下载
  84. completionHandler(NSURLSessionResponseAllow);
  85. }
  86. // 接受到数据碎片 的时候调用,调用多次
  87. - (void)URLSession:(NSURLSession *)session
  88. dataTask:(NSURLSessionDataTask *)dataTask
  89. didReceiveData:(NSData *)data {
  90. // 接收到session 下载碎片交个列队管理
  91. [self.queue dataTask:dataTask didReceiveData:data];
  92. }
  93. // <NSURLSessionDataDelegate> 完成下载
  94. - (void)URLSession:(NSURLSession *)session
  95. task:(NSURLSessionTask *)task
  96. didCompleteWithError:(nullable NSError *)error {
  97. [self.queue task:task didCompleteWithError:error];
  98. }
  99. - (void)URLSession:(NSURLSession *)session
  100. task:(NSURLSessionTask *)task
  101. needNewBodyStream:(void (^)(NSInputStream * _Nullable bodyStream))completionHandler {
  102. }
  103. - (void)URLSession:(NSURLSession *)session
  104. task:(NSURLSessionTask *)task
  105. didSendBodyData:(int64_t)bytesSent
  106. totalBytesSent:(int64_t)totalBytesSent
  107. totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  108. }
  109. #pragma mark - lazy load
  110. - (NSURLSession *)session {
  111. if (!_session) {
  112. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  113. // 设置请求超时
  114. config.timeoutIntervalForRequest = -1;
  115. // config.networkServiceType = NSURLNetworkServiceTypeVideo;
  116. config.timeoutIntervalForResource = -1;
  117. // config.TLSMaximumSupportedProtocol = kSSLProtocolAll;
  118. //_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
  119. _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
  120. }
  121. return _session;
  122. }
  123. - (mixDownloadQueue *)queue {
  124. if (!_queue) {
  125. _queue = [[mixDownloadQueue alloc] init];
  126. _queue.session = self.session;
  127. }
  128. return _queue;
  129. }
  130. @end