//1.url
NSURL *url = [NSURL URLWithString:@"http://bcs.duapp.com/chenwei520/media/music.mp3"];
//2.request(config)
//3.session
NSURLSession *session = [NSURLSession sharedSession];
//4.task
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
/**
*
* @param location 文件下载完成保存的位置
* @param response 响应头
* @param error 错误信息
*
*/
//移动下载的文件到沙盒路径下
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/music1.mp3"];
NSURL *toURL = [NSURL fileURLWithPath:filePath];
NSFileManager *manager = [NSFileManager defaultManager];
[manager moveItemAtURL:location toURL:toURL error:nil];
NSLog(@"%@", filePath);
NSLog(@"response: %@", response);
}];
//5.resume
[task resume];
NSURL *url = [NSURL URLWithString:@"http://bcs.duapp.com/chenwei520/media/music.mp3"];
//2.request(config)
//3.session
NSURLSession *session = [NSURLSession sharedSession];
//4.task
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
/**
*
* @param location 文件下载完成保存的位置
* @param response 响应头
* @param error 错误信息
*
*/
//移动下载的文件到沙盒路径下
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/music1.mp3"];
NSURL *toURL = [NSURL fileURLWithPath:filePath];
NSFileManager *manager = [NSFileManager defaultManager];
[manager moveItemAtURL:location toURL:toURL error:nil];
NSLog(@"%@", filePath);
NSLog(@"response: %@", response);
}];
//5.resume
[task resume];