NSURLSession学习笔记(二)Session Task

时间:2025-03-29 09:03:27
  • /* 使用NSURLSessionDownloadTask下载文件 */  
  • - (IBAction)downloadFile:(id)sender {  
  •     [self.spinner startAnimating];  
  •       
  •     NSURL *URL = [NSURL URLWithString:@"/image/w%3D2048/sign=6be5fc5f718da9774e2f812b8469f919/"];  
  •     NSURLRequest *request = [NSURLRequest requestWithURL:URL];  
  •     NSURLSession *session = [NSURLSession sharedSession];  
  •     NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request  
  •                                                             completionHandler:  
  •                                               ^(NSURL *location, NSURLResponse *response, NSError *error) {  
  •                                                   [self showResponseCode:response];  
  •                                                     
  •                                                   // 输出下载文件原来的存放目录  
  •                                                   NSLog(@"%@", location);  
  •                                                     
  •                                                   // 设置文件的存放目标路径  
  •                                                   NSString *documentsPath = [self getDocumentsPath];  
  •                                                   NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:documentsPath];  
  •                                                   NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:[[response URL] lastPathComponent]];  
  •                                                     
  •                                                   // 如果该路径下文件已经存在,就要先将其移除,在移动文件  
  •                                                   NSFileManager *fileManager = [NSFileManager defaultManager];  
  •                                                   if ([fileManager fileExistsAtPath:[fileURL path] isDirectory:NULL]) {  
  •                                                       [fileManager removeItemAtURL:fileURL error:NULL];  
  •                                                   }  
  •                                                   [fileManager moveItemAtURL:location toURL:fileURL error:NULL];  
  •                                                     
  •                                                   // 在webView中加载图片文件  
  •                                                   NSURLRequest *showImage_request = [NSURLRequest requestWithURL:fileURL];  
  •                                                   [self.webView loadRequest:showImage_request];  
  •                                                     
  •                                                   [self.spinner stopAnimating];  
  •                                               }];  
  •       
  •     [downloadTask resume];  
  • }  
  •   
  • /* 获取Documents文件夹的路径 */  
  • - (NSString *)getDocumentsPath {  
  •     NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  •     NSString *documentsPath = documents[0];  
  •     return documentsPath;  
  • }