关键代码:使用后台下载PDF文件

- (void)startDownloadPDF{

    NSURLSession *session = [self session];
NSString *downloadURLString = _fileURL;
NSURL *downloadURL = [NSURL URLWithString:[kGlobal getEncodeUrl:downloadURLString]];

  //使用NSMutableURLRequest进行AD认证
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:downloadURL]; NSString *authString = [[[NSString stringWithFormat:@"%@:%@", kGlobal.userInfo.sAccount, kGlobal.userInfo.sPassword] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString]; authString = [NSString stringWithFormat: @"Basic %@", authString]; [req setValue:authString forHTTPHeaderField:@"Authorization"]; NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:req]; //执行resume 保证开始了任务
[task resume]; } //后台会话(保证只有一个后台会话)
-(NSURLSession *)session {
static NSURLSession *session;
static dispatch_once_t token;
dispatch_once(&token, ^{
//后台下载
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"rich"];
configuration.discretionary = YES; //使用配置的NSURLSessionConfiguration获取NSSession,并且设置委托
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
}); return session;
}

//HTTPS认证
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{ NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
__block NSURLCredential *credential = nil; if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
if (credential) {
disposition = NSURLSessionAuthChallengeUseCredential;
} else {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
}
} else {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
} if (completionHandler) {
completionHandler(disposition, credential);
}
}

//打印下载过程,下载的百分比
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float percent = (float)totalBytesWritten/totalBytesExpectedToWrite;
NSLog(@"%f",percent);
}

//下载完成之后的文件存储在临时目录中,下载完成之后,转移文件的目录
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {
// NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// NSString *path = [dirPath stringByAppendingPathComponent:@"1.mp3"];
//
// NSFileManager *manager = [NSFileManager defaultManager];
// if ([manager fileExistsAtPath:path isDirectory:NO]) {
// [manager removeItemAtPath:path error:nil];
// }
//
// [manager moveItemAtPath:[location path] toPath:path error:nil];
} //任务结束
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {
NSLog(@"%s:%@",__func__,error);
if (error == NULL) {
self.rightButton.enabled = YES;
}
}

使用NSURLSession请求需要AD认证的HTTPS服务器的更多相关文章

  1. Web API集成Azure AD认证

    1.声明的介绍 基于角色的授权管理,适用于角色变化不大,并且用户权限不会频繁更改的场景. 在更复杂的环境下,仅仅通过给用户分配角色并不能有效地控制用户访问权限. 基于声明的授权有许多好处,它使认证和授 ...

  2. HTTP认证与https简介

    HTTP请求报头: Authorization HTTP响应报头: WWW-Authenticate  HTTP认证是基于质询/回应(challenge/response)的认证模式. HTTP认证 ...

  3. HTTP请求响应过程以及与HTTPS区别

    HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏览器)的传送通信协议.与之类似的还有文件传送协议(file transfer p ...

  4. 【Azure 事件中心】使用Azure AD认证方式创建Event Hub Consume Client + 自定义Event Position

    问题描述 当使用SDK连接到Azure Event Hub时,最常规的方式为使用连接字符串.这种做法参考官网文档就可成功完成代码:https://docs.azure.cn/zh-cn/event-h ...

  5. AD认证

    这两天接触到一个新的知识点,AD验证.什么是AD验证?Active Directory——活动目录,活动目录只是LDAP的一个实现,提供LDAP认证.Radius认证和NTML认证,都是标准认证方式 ...

  6. SharePoint2013基于Form(FBA)的AD认证登陆

    来源于:http://www.haogongju.net/art/1964313 1. 使用SharePoint2013实现基于AD的Form认证,首先创建一个Web Application,步骤如下 ...

  7. # openVPN+LDAP AD认证,组权限管理

    # openVPN+LDAP AD认证,组权限管理 原创内容http://www.cnblogs.com/elvi/p/7661178.html # openVPN+LDAP AD认证,组权限管理 # ...

  8. Git认证方式https和ssh的原理及比较

    常见的代码托管平台GitHub.GitLab和BitBucket等,基本都会使用Git作为版本控制工具.平台一般都提供两种认证方式https和ssh.了解该过程能够更加自由的配置和使用,本文就来简单聊 ...

  9. laravel5.7 前后端分离开发 实现基于API请求的token认证

    最近在学习前后端分离开发,发现 在laravel中实现前后台分离是无法无法使用 CSRF Token 认证的.因为 web 请求的用户认证是通过Session和客户端Cookie的实现的,而前后端分离 ...

随机推荐

  1. Jenkins+Maven+Svn搭建持续集成环境持续集成和自动部署

    Jenkins和Hudson有很深的渊源,Jenkins目前更新频繁,目前选用Jenkins为持续集成工具和自动部署 Jenkins的使用有很多的介绍,主要记录如下要点: 192.168.1.240: ...

  2. 每天php函数 - list()给一组变量赋值

    array list ( mixed $varname [, mixed $... ] ) 像 array() 一样,这不是真正的函数,而是语言结构.list()用一步操作给一组变量进行赋值. var ...

  3. link them together by means of pointers

    Computer Science An Overview _J. Glenn Brookshear _11th Edition An alternative to storing a heteroge ...

  4. ArrayList调用remove方法需要注意的地方

    ArrayList中有remove 方法和 removeAll方法, ArrayList中不仅继承了接口Collection中的remove方法,而且还扩展了remove方法. Collection中 ...

  5. XML xsd

    targetNamespace:表示本XSD中定义的元素和类型的名字空间都是http://exammpleOrder. xmlns:xsd:表示以前缀xsd开头的元素或则类型来自于http://www ...

  6. VS下如何调试多线程

    四步即可 1.打开多线程窗口,找到当前线程 此时,出现窗口如下: 2.右击任意位置,选中全部线程 3.停止全部线程 此时,线程状态如下: 4.单独启动当前线程:先单击当前线程,在点击启动按钮,如下红色 ...

  7. Android生命周期详细说明

    提供两个关于Activity的生命周期模型图示帮助理解:                                           图1 图2 从图2所示的Activity生命周期不难看出, ...

  8. Gulp自动化工具之图片压缩

    一.安装node https://nodejs.org/download/ 根据需要选择对应的版本 安装好了之后可以通过node -v参看一下版本 node -v 二.安装gulp npm insta ...

  9. JavaScript学习之窗口

    窗口 一.Window 对象 Window 对象表示浏览器中打开的窗口.如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建 ...

  10. Swift-07-析构器deinit

    析构器只适用于类类型,当一个类的实例被释放之前,析构器会被立即调用.析构器用关键字deinit来标识,类似于构造器用init来标识. 原理: Swift会自动释放不再需要的实例以释放资源.Swift通 ...