1.实施步骤
1.1 使用 NSURLSession对象 创建TASK ,然后执行TASK
2.TASK的类型:
- (void)download{ NSURL*url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_15.png"]; // NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSession *session = [NSURLSession sharedSession]; // 创建TASK 下载到location中 NSURLSessionDownloadTask *tast = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%s",__func__); NSFileManager *fileManger = [NSFileManager defaultManager]; // 设置下载的文件路径和文件名 NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:response.suggestedFilename]; [fileManger moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil]; NSLog(@"%@",file); }]; [tast resume]; }
2016 - 1 - 24 NSURLSession (一)
原文:http://www.cnblogs.com/BJTUzhengli/p/5156089.html