精准的控制任务的取消, 挂起, 恢复
#pragma mark --- 请求数据 request --- session ---- sessionDataTask
- (void)loadData
{
// 1. 创建url
NSString *urlString = [NSString stringWithFormat:@"url"];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
// 2. 创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. 创建会话 (使用单例初始化, 启动任务)
NSURLSession *session = [NSURLSession sharedSession];
// 会话创建任务
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", dataStr);
} else {
NSLog(@"error is %@", error.localizedDescription);
}
}];
// 恢复线程, 启动任务
[dataTask resume];
}
#pragma mark ----- 文件上传
- (void)upDataFile
{
/**
* 文件上传的时候需要设置请求头中Content-Type类型, 必须使用URL编码,
application/x-www-form-urlencoded:默认值,发送前对所有发送数据进行url编码,支持浏览器访问,通常文本内容提交常用这种方式。
multipart/form-data:多部分表单数据,支持浏览器访问,不进行任何编码,通常用于文件传输(此时传递的是二进制数据) 。
text/plain:普通文本数据类型,支持浏览器访问,发送前其中的空格替换为“+”,但是不对特殊字符编码。
application/json:json数据类型,浏览器访问不支持 。
text/xml:xml数据类型,浏览器访问不支持。
multipart/form-data 必须进行设置,
*/
// 1. 创建URL
NSString *urlStr = @"url";
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlStr];
// 2. 创建请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求的为POST
request.HTTPMethod = @"POST";
// 3.构建要上传的数据
NSString *path = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"123"];
NSData *data = [NSData dataWithContentsOfFile:path];
// 设置request的body
request.HTTPBody = data;
// 设置请求 Content-Length
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];
// 设置请求 Content-Type
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",@"Xia"] forHTTPHeaderField:@"Content-Type"];
// 4. 创建会话
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
// 上传成功
}else {
// 上传失败, 打印error信息
NSLog(@"error --- %@", error.localizedDescription);
}
}];
// 恢复线程 启动任务
[uploadTask resume];
}
#pragma mark --- 文件下载
/**
* 使用NSURLSessionDownloadTask下载文件过程中注意:
下载文件之后会自动保存到一个临时目录中, 需要自己将文件重新放到其他指定的目录中
*/
- (void)downLoadFile
{
// 1. 创建url
NSString *urlStr =[NSString stringWithFormat:@"%@", @"url"];
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *Url = [NSURL URLWithString:urlStr];
// 创建请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url];
// 创建会话
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *downLoadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
// 下载成功
// 注意 location是下载后的临时保存路径, 需要将它移动到需要保存的位置
NSError *saveError;
// 创建一个自定义存储路径
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *savePath = [cachePath stringByAppendingPathComponent:@"fileName"];
NSURL *saveURL = [NSURL fileURLWithPath:savePath];
// 文件复制到cache路径中
[[NSFileManager defaultManager] copyItemAtURL:location toURL:saveURL error:&saveError];
if (!saveError) {
NSLog(@"保存成功");
} else {
NSLog(@"error is %@", saveError.localizedDescription);
}
} else {
NSLog(@"error is : %@", error.localizedDescription);
}
}];
// 恢复线程, 启动任务
[downLoadTask resume];
}
#pragma mark -- 取消下载
-(void)cancleDownLoad
{
[_downloadTask cancel];
}
#pragma mark --- 挂起下载
- (void)suspendDownload
{
[_downloadTask suspend];
}
#pragma mark ---- 恢复继续下载
- (void)resumeDownLoad
{
[_downloadTask resume];
}
#pragma mark ---- downLoadTask 代理方法
// 下载过程中 会多次调用, 记录下载进度
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
// 记录下载进度
}
// 下载完成
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSError *error;
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *savePath = [cachePath stringByAppendingPathComponent:@"savename"];
NSURL *saveUrl = [NSURL fileURLWithPath:savePath];
// 通过文件管理 复制文件
[[NSFileManager defaultManager] copyItemAtURL:location toURL:saveUrl error:&error];
if (error) {
NSLog(@"Error is %@", error.localizedDescription);
}
}
// 当调用恢复下载的时候 触发的代理方法 [_downLoadTask resume]
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
}
#pragma mark --- 任务完成, 不管是否下载成功
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
}
#pragma mark --- session 后台下载完成 之后的操作 (本地通知 或者 更新UI)
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (appdelgate.backgroundSessionCompletionHandler) {
void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;
appdelgate.backgroundSessionCompletionHandler = nil;
completionHandle();
}
}
NSURLSession 支持程序的后台下载和上传, 苹果官方将其称之进程之外的上传和下载, 这些任务都交给后台守护线程完成, 而不是应用本身, 及时文件在下载和上传过程中崩溃了也可以继续运行(如果用户强制关闭程序的话, NSURLSession会断开连接)
*
* 为了提高用户体验, 下载过程中进度条会一直刷新进度,
当程序进入后台后, 事实上任务是交给iOS系统来调度的, 具体什么时候下载完成就不知道了,
如果下载完成之后, 文件下载进度应该在100位置, 由于程序已经在后台无法更新程序UI,
此时通过应用程序代理 (AppDelegate)方法进行UI更新
当NSURLSession在后台开启几个任务之后, 如果有其他几个任务完成后系统会调用应用程序的
-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)())completionHandler代理方法
此方法会包含一个competionHandler (此操作表示应用中完成所有处理工作), 通常我们会保存此对象,
直到最后一个任务完成, 此时重新通过会话标识(sessionConfig中设置的)找到相应的会话并调用NSURLSession的
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session代理方法, 在这个方法中通常可以
进行UI更新,并调用completionHandler通知系统已经完成所有的操作
#pragma mark --- 后台下载
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
self.backgroundSessionCompletionHandler = completionHandler;
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
{
AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (appdelgate.backgroundSessionCompletionHandler) {
void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;
appdelgate.backgroundSessionCompletionHandler = nil;
completionHandle();
}
}