#import "ViewController.h"
#import "SSZipArchive.h"
@interface ViewController ()<NSURLSessionDownloadDelegate>
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property (nonatomic, strong) NSURLSession *session; // 自定义会话
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask; // 下载任务
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",NSHomeDirectory());
// Do any additional setup after loading the view, typically from a nib.
}
// 在暂停或者继续之前先判断当前的任务状态
- (IBAction)pause:(id)sender {
// 只有在运行状态的才能暂停
if (self.downloadTask.state == NSURLSessionTaskStateRunning) {
NSLog(@"暂停");
[self.downloadTask suspend];
}
}
- (IBAction)resume:(id)sender {
if (self.downloadTask.state == NSURLSessionTaskStateSuspended) {
NSLog(@"继续");
[self.downloadTask resume];
}
}
// 使用NSURLSession 下载,内存锋值不高。但是有些Xcode版本,峰值很6.3以前
// 但是在真机上不存在这个问题
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// NSURL
NSURL *url = [NSURL URLWithString:@"http://dlsw.baidu.com/sw-search-sp/soft/c6/25790/WeChatzhCN1.0.0.6.1428545414.dmg"];
// NSURL *url = [NSURL URLWithString:@"http://localhost/09.zip"];
// Session发起请求
// location 文件下载完这后保存的路径
// 单例可以在多个控制器使用
// [[self.session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// NSLog(@"%@ -- %@",location.path, response);
// // 下载完文件之后,文件被删除了
// // 因为系统以为你下载的是压缩包,下载完成之后需要解压。解压完成之后,删除原文件
// // 解压
// /*
// 1. 压缩包的路径
// 2. 解压到的路径
// */
[SSZipArchive unzipFileAtPath:location.path toDestination:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]];
//
// // 下载视频,需要解压吗 不需要
// // 复制一份到另一个目录
// NSFileManager *manager = [NSFileManager defaultManager];
// // 保存文件的路径
// NSString *fileName = [url.path lastPathComponent];
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:fileName];
//
// // 把临时文件移动到缓存目录,并且改成我们需要的后缀名
// // 使用复制或者移动都可以
[manager moveItemAtPath:location.path toPath:path error:NULL];
// [manager copyItemAtPath:location.path toPath:path error:NULL];
// }] resume];
// 防止点击多次
// 下载任务
self.downloadTask = [self.session downloadTaskWithURL:url];
// 继续(开始)下载
[self.downloadTask resume];
}
#pragma mark - Session 的代理
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
// NSLog(@"%@",location);
NSLog(@"下载完成,开始移动文件");
// 处理下载完成的文件
// NSFileManager *manager = [NSFileManager defaultManager];
// // 保存文件的路径
// NSString *fileName = @"123.mp4";
// NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:fileName];
//
// // 把临时文件移动到缓存目录,并且改成我们需要的后缀名
// // 使用复制或者移动都可以
// // [manager moveItemAtPath:location.path toPath:path error:NULL];
// [manager copyItemAtPath:location.path toPath:path error:NULL];
}
// 此方法并没什么卵用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
NSLog(@"didResumeAtOffset");
}
/**
session 会话
downloadTask 任务的对象
bytesWritten 本次写入的数据长度
totalBytesWritten 总共接收了数据长度
totalBytesExpectedToWrite 总的数据长度
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
//得到下载进度
CGFloat progress = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite;
NSLog(@"%f - %@",progress,[NSThread currentThread]);
//回到主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.progress = progress;
});
}
- (NSURLSession *)session {
if (_session == nil) {
// 创建配置对象 通常使用默认的配置就可以了
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
/*
delegateQueue
如果设置nil , 代理在子线程回调,串行队列
[[NSOperationQueue alloc]init] 还是在同一时间内只开启一条线程
[NSOperationQueue mainQueue] 在主线程回调
*/
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}
return _session;
}
@end
网络:NSURLSession 下载暂停与继续
最新推荐文章于 2020-05-10 09:44:04 发布