//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];
本文介绍了一个使用Swift语言在iOS环境下从指定URL下载音乐文件的例子,并详细展示了如何将下载的文件移动到应用沙盒目录的过程。
2411

被折叠的 条评论
为什么被折叠?



