mixDownloadSession.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. - (void)startAllDownloads {
  23. [self.queue startAllTasksWithSession:self.session];
  24. }
  25. #pragma mark - <NSURLSessionDataDelegate>
  26. // ssl 服务 证书信任
  27. - (void)URLSession:(NSURLSession *)session
  28. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  29. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
  30. if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
  31. return;
  32. }
  33. // 信任该插件
  34. NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  35. // 第一个参数 告诉系统如何处置
  36. completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
  37. }
  38. //当请求协议是https的时候回调用该方法
  39. //Challenge 挑战 质询(受保护空间)
  40. //NSURLAuthenticationMethodServerTrust 服务器信任证书
  41. - (void)URLSession:(NSURLSession *)session
  42. task:(NSURLSessionTask *)task
  43. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  44. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
  45. if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
  46. return;
  47. }
  48. // 信任该插件
  49. NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  50. // 第一个参数 告诉系统如何处置
  51. completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
  52. }
  53. // 接受到响应调用
  54. - (void)URLSession:(NSURLSession *)session
  55. dataTask:(NSURLSessionDataTask *)dataTask
  56. didReceiveResponse:(NSURLResponse *)response
  57. completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
  58. // 将响应交给列队处理
  59. [self.queue dataTask:dataTask didReceiveResponse:response];
  60. // 允许下载
  61. completionHandler(NSURLSessionResponseAllow);
  62. }
  63. // 接受到数据碎片 的时候调用,调用多次
  64. - (void)URLSession:(NSURLSession *)session
  65. dataTask:(NSURLSessionDataTask *)dataTask
  66. didReceiveData:(NSData *)data {
  67. // 接收到session 下载碎片交个列队管理
  68. [self.queue dataTask:dataTask didReceiveData:data];
  69. }
  70. // <NSURLSessionDataDelegate> 完成下载
  71. - (void)URLSession:(NSURLSession *)session
  72. task:(NSURLSessionTask *)task
  73. didCompleteWithError:(nullable NSError *)error {
  74. [self.queue task:task didCompleteWithError:error];
  75. }
  76. - (void)URLSession:(NSURLSession *)session
  77. task:(NSURLSessionTask *)task
  78. needNewBodyStream:(void (^)(NSInputStream * _Nullable bodyStream))completionHandler {
  79. }
  80. - (void)URLSession:(NSURLSession *)session
  81. task:(NSURLSessionTask *)task
  82. didSendBodyData:(int64_t)bytesSent
  83. totalBytesSent:(int64_t)totalBytesSent
  84. totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  85. }
  86. #pragma mark - lazy load
  87. - (NSURLSession *)session {
  88. if (!_session) {
  89. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  90. // 设置请求超时
  91. config.timeoutIntervalForRequest = -1;
  92. // config.networkServiceType = NSURLNetworkServiceTypeVideo;
  93. config.timeoutIntervalForResource = -1;
  94. // config.TLSMaximumSupportedProtocol = kSSLProtocolAll;
  95. _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
  96. }
  97. return _session;
  98. }
  99. - (mixDownloadQueue *)queue {
  100. if (!_queue) {
  101. _queue = [[mixDownloadQueue alloc] init];
  102. _queue.session = self.session;
  103. }
  104. return _queue;
  105. }
  106. @end