iOS 下载 后台下载 断点下载

2019-05-27  本文已影响0人  Rumbles

1.https

 服务器的认证方法  authenticationMethod
 
 NSURLProtectionSpaceHTTP                     ///> HTTP协议
 NSURLProtectionSpaceHTTPS                    ///> HTTPS协议
 NSURLProtectionSpaceFTP                      ///> FTP协议
 
 NSURLProtectionSpaceHTTPProxy                ///> http代理的代理类型
 NSURLProtectionSpaceHTTPSProxy               ///> HTTPS代理的代理类型
 NSURLProtectionSpaceFTPProxy                 ///> FTP代理的代理类型
 NSURLProtectionSpaceSOCKSProxy               ///> SOCKS代理的代理类型
 
 NSURLAuthenticationMethodDefault             ///> 协议的默认身份验证方法
 NSURLAuthenticationMethodHTTPBasic           ///> HTTP基本身份验证。相当于http的NSURLAuthenticationMethodDefault
 需要一个用户名和密码。使用  [NSURLCredential credentialWithUser:@"" password:@"" persistence:@""]
 
 NSURLAuthenticationMethodHTTPDigest          ///> HTTP摘要身份验证。使用
 credentialWithUser:password:persistence:方法创建NSURLCredential对象
 
 
 NSURLAuthenticationMethodHTMLForm            ///> HTML表单认证。适用于任何协议   基于用户名/密码的认证
 NSURLAuthenticationMethodNTLM                ///> NTLM身份验证。              基于用户名/密码的认证
 NSURLAuthenticationMethodNegotiate           ///> 协商认证                    基于用户名/密码的认证
 
 上面的认证好像都需要用户名和密码
 
 NSURLAuthenticationMethodClientCertificate   ///> SSL客户端证书。适用于任何协议。
 需要system identity和需要与server进行身份验证的所有证书。使用credentialWithIdentity:certificates:persistence:创建NSURLCredential对象
 

 NSURLAuthenticationMethodServerTrust         ///> 需要SecTrustRef验证。适用于任何协议
 需要authentication challenge的protection space提供一个trust。使用credentialForTrust:来创建NSURLCredential对象。

必须实现这个方法
///> 只要请求的地址是HTTPS的, 就会调用这个代理方法
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
    
    NSLog(@"服务器要求验证客户端身份 ");
    
    // 1.从服务器返回的受保护空间中拿到证书的类型  获取服务器的认证方法 服务器的认证方法
    NSString *method = challenge.protectionSpace.authenticationMethod;
    
    // 2.判断服务器返回的证书是否是服务器信任的
    if([method isEqualToString:NSURLAuthenticationMethodServerTrust]){
        // 3.根据服务器返回的受保护空间创建一个证书
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 4.安装证书
        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
        return;
    }
}

2.断点下载 记得在app启动的时候就应该调用设置代理

///> 当task完成的时候调用
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
    NSLog(@" 当task完成的时候调用 %tu  ",task.taskIdentifier);
    if (error) {
        if ([error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData]) {
            NSData *resumeData = [error.userInfo objectForKey:NSURLSessionDownloadTaskResumeData];
            
            self.model = [self getDownloadModelWithDownloadUrl:task.currentRequest.URL.absoluteString];
            
            if (self.model) {
                self.model.downloadTask = [self.session downloadTaskWithResumeData:resumeData];
                [self.model.downloadTask resume];
                NSLog(@"断点下载:URL:%@",self.model.downloadTask.currentRequest.URL.absoluteString);
            } else {
                NSLog(@"断点下载:model为空 URL:%@",task.currentRequest.URL.absoluteString);
            }
        }
    }
}

3.后台下载

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler {
    NSLog(@"%s",__func__);
    [HJDownLoadManager sharedManager].backgroundSessionCompletionHandler = completionHandler;
}
///> 这个方法在我们写后台下载的Demo中我们是会遇到的
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
    if (self.backgroundSessionCompletionHandler) {
        self.backgroundSessionCompletionHandler();
    }
    NSLog(@"%s",__func__);
}

Demo

上一篇 下一篇

猜你喜欢

热点阅读