1 // 下载结束之后,会调用
2 // location 临时文件保存目录
3 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
4 didFinishDownloadingToURL:(NSURL *)location
5 {
6 NSString *path = @"/Users/apple/Desktop/QQQ.dmg";
7
8 [[NSFileManager defaultManager] copyItemAtPath:location.path toPath:path error:NULL];
9 }
10 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
11 didWriteData:(int64_t)bytesWritten
12 totalBytesWritten:(int64_t)totalBytesWritten
13 totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
14 {
15 // 检测下载进度
16
17 float progress = (float) totalBytesWritten/totalBytesExpectedToWrite;
18
19 // 回到主线程 检测网络下载情况
20 dispatch_async(dispatch_get_main_queue(), ^{
21 //
22 self.progress.progress = progress;
23 });
24
25 }
26 // 文件操作 不会造成内存飙升.文件剪切的操作
27 //[[NSFileManager defaultManager] moveItemAtPath:location.path toPath:path error:NULL];
28
29 // 文件拷贝 如果涉及到 url的文件操作,都会导致内存飙升.
30 [[NSFileManager defaultManager] copyItemAtPath:location.path toPath:path error:NULL];
31
32 // 下面这种文件操作方式.会造成内存飙升.
33 // [[NSFileManager defaultManager] moveItemAtURL:location toURL:url error:NULL];
34 }];
35