-
Notifications
You must be signed in to change notification settings - Fork 2
/
DownloadTask.m
executable file
·157 lines (122 loc) · 5.81 KB
/
DownloadTask.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// DownloadTask.m
// NetworkingCraft
//
// Created by YouXianMing on 15/6/11.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import "DownloadTask.h"
#import "AFNetworking.h"
@interface DownloadTask ()
@property (nonatomic, strong) AFHTTPSessionManager *session;
@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSString *file;
@end
@implementation DownloadTask
- (instancetype)init {
self = [super init];
if (self) {
[self defaultConfig];
}
return self;
}
- (void)defaultConfig {
self.session = [AFHTTPSessionManager manager];
}
- (void)startDownload {
if (self.urlString.length <= 0) {
return;
}
// 定义一个progress指针
NSProgress *progress;
// 创建一个URL链接
NSURL *url = [NSURL URLWithString:self.urlString];
// 初始化一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 开始下载任务
__weak DownloadTask *weakSelf = self;
self.downloadTask = \
[self.session downloadTaskWithRequest:request
progress:&progress
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
// 获取基本路径
NSString *filePath = nil;
// 文件路径
if (weakSelf.filePath) {
filePath = [weakSelf pathWithSting:self.filePath];
} else {
filePath = [weakSelf pathWithSting:@"/Library/Caches"];
}
// 文件名字
if (weakSelf.fileName) {
filePath = [filePath stringByAppendingPathComponent:weakSelf.fileName];
} else {
filePath = [filePath stringByAppendingPathComponent:[response suggestedFilename]];
}
// 获取文件绝对路径
weakSelf.file = filePath;
NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:filePath];
return documentsDirectoryURL;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (weakSelf.delegate) {
// 结束后移除掉这个progress
[progress removeObserver:self
forKeyPath:@"fractionCompleted"
context:nil];
if (error == nil) {
// 成功
if ([weakSelf.delegate respondsToSelector:@selector(downloadTaskSucess:)]) {
[weakSelf.delegate downloadTaskSucess:weakSelf];
}
} else {
// 出错
if ([weakSelf.delegate respondsToSelector:@selector(downloadTask:failedWithError:)]) {
[weakSelf.delegate downloadTask:weakSelf failedWithError:error];
}
}
}
}];
// 开始下载
[self.downloadTask resume];
if (self.delegate) {
// 给这个progress添加监听任务
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:nil];
}
}
- (void)stopDownload {
[self.downloadTask cancel];
}
/**
* 获取路径
*
* @param string 相对路径
*
* @return 绝对路径
*/
- (NSString *)pathWithSting:(NSString *)string {
return [NSHomeDirectory() stringByAppendingPathComponent:string];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:@"fractionCompleted"] && [object isKindOfClass:[NSProgress class]]) {
NSProgress *progress = (NSProgress *)object;
[_delegate downloadTask:self withProgress:progress.fractionCompleted];
}
}
+ (instancetype)downloadTaskWithUrlString:(NSString *)urlString
fileDirectory:(NSString *)filePath
fileName:(NSString *)fileName
delegate:(id)delegate {
DownloadTask *downloadTask = [[[self class] alloc] init];
downloadTask.urlString = urlString;
downloadTask.filePath = filePath;
downloadTask.fileName = fileName;
downloadTask.delegate = delegate;
return downloadTask;
}
@end