1. 不需要獲取文件下載進度的方法
// 此方法無法獲取到文件的下載進度,只能在結束時做響應
- (void)download {
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/HBuilder.zip"];
[[[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 當前線程, 子線程
NSLog(@"%@", [NSThread currentThread]);
// 當前文件位置,臨時文件,下載完成后刪除臨時文件
NSLog(@"%@",location);
// 將文件復制到自己想要的位置
// 存放文件的路徑
NSString *doc = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Builder.zip"];
// 復制文件
[[NSFileManager defaultManager] copyItemAtPath:location.path toPath:doc error:NULL];
}] resume];
}
2. 需要獲取文件下載進度
需要給NSURLSession設置代理, ViewController并實現NSURLSessionDownloadDelegate協議,根據需求重寫協議中的方法。
#import "ViewController.h"
@interface ViewController () <NSURLSessionDownloadDelegate>
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSData *resumData;
@end
@implementation ViewController
// 開始下載
- (IBAction)start {
[self download];
}
// 暫停下載
- (IBAction)pause {
[self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
// 記錄已下載的數據
self.resumData = resumeData;
// 把續傳數據保存到沙盒中
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"123.tmp"];
[self.resumData writeToFile:path atomically:YES];
NSLog(@"%@", path);
// 將下載任務置為空
self.downloadTask = nil;
}];
}
// 繼續下載
- (IBAction)resume {
// 從沙盒中獲取續傳數據
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"123.tmp"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
self.resumData = [NSData dataWithContentsOfFile:path];
}
if (self.resumData == nil) {
return;
}
// 調用斷點下載方法
self.downloadTask = [self.session downloadTaskWithResumeData:self.resumData];
[self.downloadTask resume];
// 將續傳數據置為空
self.resumData = nil;
}
// 懶加載
- (NSURLSession *)session {
if (_session == nil) {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:0];
}
return _session;
}
// 下載,設置代理,獲取進度
- (void)download {
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/HBuilder.zip"];
self.downloadTask = [self.session downloadTaskWithURL:url];
// 開始下載
[self.downloadTask resume];
}
// 寫數據
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float process = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
NSLog(@"%f", process);
}
// 斷點下載數據
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
NSLog(@"續傳");
}
// 下載完成
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSLog(@"%@", [NSThread currentThread]);
NSLog(@"下載完成 %@", location);
}
@end