標籤:網路 應用 pat mic write use 進度 功能 nat
1、大檔案下載:1-1、建立下載任務:方式一:建立時同時設定代理監聽下載進度:
1.-(void)downloadDelegate
2.{
3. //01 確定資源路徑
4. NSURL *url = [NSURL URLWithString:@"http://img4q.duitang.com/uploads/item/201406/09/20140609150919_ZztLd.jpeg"];
5.
6. //02 建立請求對象
7. NSURLRequest *request = [NSURLRequest requestWithURL:url];
8.
9. //03 建立會話對象 設定代理
10. NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
11.
12. //04 建立downloadTask
13. NSURLSessionDownloadTask *downloadTask =[session downloadTaskWithRequest:request];
14.
15. //05 發送請求
16. [downloadTask resume];
17.
18.}
方式二:直接建立NSURLSessionDownloadTask對象:
1.//缺點:無法監聽檔案的進度
2.-(void)download
3.{
4. //01 確定資源路徑
5. NSURL *url = [NSURL URLWithString:@"http://img4q.duitang.com/uploads/item/201406/09/20140609150919_ZztLd.jpeg"];
6.
7. //02 建立請求對象
8. NSURLRequest *request = [NSURLRequest requestWithURL:url];
9.
10. //03 建立會話對象
11. NSURLSession *session = [NSURLSession sharedSession];
12.
13. //04 建立downloadTask
14. /* 參數說明
15. *
16. * 第一個參數:請求對象
17. * 第二個參數:completionHandler 請求完成(成功|失敗)的時候調用
18. * location:位置 檔案的位置 內部已經實現了邊接收資料邊寫沙箱的操作
19. * response:回應標頭資訊
20. */
21. NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
22. //預設已經把資料寫到磁碟中:tmp/...隨時可能被刪除
23.
24. //轉移檔案(轉移到安全的地方去)
25. NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
26. NSString *fullPath = [cachePath stringByAppendingPathComponent:response.suggestedFilename];
27.
28. //路徑 ->NSURL
29. //URLWithString 不做其他額外的處理
30. //fileURLWithPath
31. NSLog(@"%@",[NSURL URLWithString:fullPath]);
32. NSLog(@"%@",[NSURL fileURLWithPath:fullPath]);
33.
34. [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
35. }];
36.
37. //05 發送請求
38. [downloadTask resume];
39.}
1-2、NSURLSessionDownloadTask常用代理方法:
1.#pragma mark NSURLSessionDownloadDelegate
2.
3.//01 寫資料的時候調用
4.// bytesWritten 本次寫入的資料大小
5.// totalBytesWritten 寫入資料的總大小
6.// totalBytesExpectedToWrite 檔案的總大小
7.-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
8.{
9. NSLog(@"%f",1.0 * totalBytesWritten / totalBytesExpectedToWrite);
10.}
?重要:開始嚮應用沙箱中預設路徑下的檔案開始寫入資料時調用。
1.//02 下載完成的時候調用
2.-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
3.{
4. //轉移檔案(轉移到安全的地方去)
5. NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
6. NSString *fullPath = [cachePath stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
7.
8. //剪下檔案
9. [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
10.}
?重要:下載完成時調用該方法,預設下載完成便會刪除該路徑下的檔案;所以,需要在下載完成時將檔案轉移至其它安全的路徑下。
1.//03 整個請求結束或者是失敗的時候調用
2.-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
3.{
4. NSLog(@"didCompleteWithError");
5.}
?重要:當下載任務完成時或失敗時調用。
2、斷點續傳:
?重要:1、NSURLSessionDownloadTask能使用suspend(掛起下載任務)、resume(啟動下載任務)、cancel(不可恢複取消下載任務)、cancelByProducingResumeData:(可恢複下載任務)操作下載任務。
?重要:2、NSURLSessionDownloadTask使用cancel方法取消下載操作之後便不能夠再恢複下載任務(即:使用cancel方法取消之後便不能再進行斷點續傳,因為,downloadTask如果不做特殊處理,下載操作執行完畢或者使用cancel方法取消將會立即刪除儲存在沙箱路徑中的檔案;所以,不能使用NSURLSessionDownloadTask實現離線下載功能,也不能使用cancel方法實現斷點續傳功能)。
?重要:3、使用cancelByProducingResumeData:方法取消下載任務,使用屬性儲存resumeData資料;便可以實現斷點續傳功能。
?重要:4、resumeData:可以用來恢複下載的資料,並不是沙箱中儲存的已經下載好的檔案資料。
2-1、使用屬性儲存恢複下載操作所需的
resumeData資料:
[email protected] (nonatomic, strong) NSData *resumeData;
2-2、使用
cancelByProducingResumeData:暫停下載操作:
1.- (IBAction)cacnelBtnClick:(id)sender
2.{
3. //取消 普通的取消操作是不可以恢複的
4. //[self.downloadTask cancel];
5.
6. //取消,可以恢複的取消操作
7. //resumeData 可以用來恢複下載的資料 並不是沙箱中儲存的已經下載好的檔案資料
8. [self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
9. self.resumeData = resumeData;
10. }];
11.
12. self.downloadTask = nil;
13. NSLog(@"取消下載任務+++");
14.}
2-3、恢複下載任務:
1.- (IBAction)resumeBtnClick:(id)sender
2.{
3. //暫停->恢複
4. if(self.resumeData)
5. {
6. //取消->恢複
7. //在恢複下載的時候,判斷是否有可以用來進行恢複下載的資料,如果有那麼就根據該資料建立一個新的網路請求
8. self.downloadTask = [self.session downloadTaskWithResumeData:self.resumeData];
9. self.resumeData = nil;
10. }
11.
12. [self.downloadTask resume];
13.}
iOS核心筆記——網路編程-NSURLSessionDownloadTask