123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- //
- // mixDownloadSession.m
- // 双子星云手机
- //
- // Created by xd h on 2024/6/27.
- //
- #import "mixDownloadSession.h"
- #import "mixDownloadQueue.h"
- #import "mixDownloadOperation.h"
- @interface mixDownloadSession () <NSURLSessionDataDelegate>
- /** session 可以支持多个任务下载,创建一次就可以 */
- @property (nonatomic,strong) NSURLSession *session;
- /** 下载列队管理 专门负责接收到数据时分配给不同operation */
- @property (nonatomic,strong) mixDownloadQueue *queue;
- @end
- @implementation mixDownloadSession
- // 添加任务
- - (void)downloadWithURL:(NSURL *)url begin:(void(^)(NSString *))begin progress:(void(^)(NSInteger,NSInteger))progress complete:(void(^)(NSDictionary *,NSError *))complet {
- // 交给列队管理
- [self.queue addDownloadWithSession:self.session URL:url begin:begin progress:progress complete:complet];
-
- }
- - (void)startAllDownloads {
- [self.queue startAllTasksWithSession:self.session];
- }
- #pragma mark - <NSURLSessionDataDelegate>
- // ssl 服务 证书信任
- - (void)URLSession:(NSURLSession *)session
- didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
- completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
- if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
- return;
- }
-
- // 信任该插件
- NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
- // 第一个参数 告诉系统如何处置
- completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
- }
- //当请求协议是https的时候回调用该方法
- //Challenge 挑战 质询(受保护空间)
- //NSURLAuthenticationMethodServerTrust 服务器信任证书
- - (void)URLSession:(NSURLSession *)session
- task:(NSURLSessionTask *)task
- didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
- completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
-
- if(![challenge.protectionSpace.authenticationMethod isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
- return;
- }
-
- // 信任该插件
- NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
- // 第一个参数 告诉系统如何处置
- completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
-
- }
- // 接受到响应调用
- - (void)URLSession:(NSURLSession *)session
- dataTask:(NSURLSessionDataTask *)dataTask
- didReceiveResponse:(NSURLResponse *)response
- completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
-
- // 将响应交给列队处理
- [self.queue dataTask:dataTask didReceiveResponse:response];
-
- // 允许下载
- completionHandler(NSURLSessionResponseAllow);
- }
- // 接受到数据碎片 的时候调用,调用多次
- - (void)URLSession:(NSURLSession *)session
- dataTask:(NSURLSessionDataTask *)dataTask
- didReceiveData:(NSData *)data {
- // 接收到session 下载碎片交个列队管理
- [self.queue dataTask:dataTask didReceiveData:data];
- }
- // <NSURLSessionDataDelegate> 完成下载
- - (void)URLSession:(NSURLSession *)session
- task:(NSURLSessionTask *)task
- didCompleteWithError:(nullable NSError *)error {
- [self.queue task:task didCompleteWithError:error];
- }
- - (void)URLSession:(NSURLSession *)session
- task:(NSURLSessionTask *)task
- needNewBodyStream:(void (^)(NSInputStream * _Nullable bodyStream))completionHandler {
-
- }
- - (void)URLSession:(NSURLSession *)session
- task:(NSURLSessionTask *)task
- didSendBodyData:(int64_t)bytesSent
- totalBytesSent:(int64_t)totalBytesSent
- totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
-
- }
- #pragma mark - lazy load
- - (NSURLSession *)session {
- if (!_session) {
- NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
- // 设置请求超时
- config.timeoutIntervalForRequest = -1;
- // config.networkServiceType = NSURLNetworkServiceTypeVideo;
- config.timeoutIntervalForResource = -1;
- // config.TLSMaximumSupportedProtocol = kSSLProtocolAll;
-
- _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
- }
-
- return _session;
- }
- - (mixDownloadQueue *)queue {
- if (!_queue) {
- _queue = [[mixDownloadQueue alloc] init];
- _queue.session = self.session;
- }
- return _queue;
- }
- @end
|