图:
gitHub地址:https://github.com/wl356485255/DownloadTaskDemo.git
注意点:1.下载的文件会保存在沙盒目录下的tmp文件夹内,这个文件夹专门存储一些临时文件,我们需要在下载完成后把文件移动到自己需要的文件夹内,并修改文件的扩展名
2.有部分链接使用下载时resume data = nil;这样断点续传功能就没有用了;
在storyboard里面添加button,progressView和显示进度的label,并进行连线
ViewController.m代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#import "ViewController.h"@interface ViewController ()<NSURLSessionDownloadDelegate>{ NSURLSessionDownloadTask *downTask; NSData *myResumeData; NSURLSession *session; NSString *fielName; }@property (weak, nonatomic) IBOutlet UIProgressView *progressView;@property (weak, nonatomic) IBOutlet UILabel *progressLabel;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];}//创建下载任务- (IBAction)downloadAction:(id)sender{ if (downTask) { return; } //构造需要下载的url链接 NSURL *url = [NSURL URLWithString:@"http://apk500.bce.baidu-mgame.com/game/883000/883227/20160127061354_oem_5004211.apk?r=1"]; //设置保存文件名为url的最后部分 fielName = [url lastPathComponent]; //设置session工作类型为默认 NSURLSessionConfiguration *sessionCf = [NSURLSessionConfiguration defaultSessionConfiguration]; //用sessionConfig配置session session = [NSURLSession sessionWithConfiguration:sessionCf delegate:self delegateQueue:[NSOperationQueue mainQueue]]; //设置任务类型 downTask = [session downloadTaskWithURL:url]; //发送任务请求 [downTask resume]; }//暂停下载任务- (IBAction)pauseAction:(UIButton *)sender{ if (downTask.state == NSURLSessionTaskStateRunning) { [downTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) { //If resume data cannot be created, the completion handler will be called with nil resumeData myResumeData = resumeData; downTask = nil; }]; } }//恢复下载任务- (IBAction)resumeAction:(id)sender{ if (myResumeData) { downTask = [session downloadTaskWithResumeData:myResumeData]; [downTask resume]; myResumeData = nil; }}//把下载来的文件移动到Documents文件夹下并修改扩展名- (void)moveFileToDocuments:(NSURL *)location{ NSFileManager *manager = [NSFileManager defaultManager]; NSString *newPath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fielName]]; fielName = nil; NSLog(@"%@",newPath); [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:newPath] error:nil]; }#pragma mark - NSURLSessionDownloadDelegate//在下载完成后调用- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{ NSLog(@"%@",location); [self moveFileToDocuments:location];}//进度条相关设置- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{ float progress = (float)totalBytesWritten / totalBytesExpectedToWrite; self.progressView.progress = progress; self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress* 100]; } |