方法一:可以直接下載一個文件
- (void)downloadFile{
NSString *urlStr = @"http://news.iciba.com/admin/tts/2013-11-15.mp3";
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSError *saveError;
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *savePath = [cachePath stringByAppendingPathComponent:@"ceshi.mp3"];
NSURL *saveUrl = [NSURL fileURLWithPath:savePath];
//把下載的內容從cache復制到document下
[[NSFileManager defaultManager] copyItemAtURL:location toURL:saveUrl error:&saveError];
if (!saveError) {
NSLog(@"save success");
}else{
NSLog(@"save error:%@",saveError.localizedDescription);
}
}else{
NSLog(@"download error:%@",error.localizedDescription);
}
}];
[downloadTask resume];
}
方法二:可以知道下載進度
加載代理方法<NSURLSessionDownloadDelegate>
把url傳給這個方法
- (void)downloadFileWithURL:(NSString *)urlStr{
//默認配置
NSURLSessionConfiguration *configuration= [NSURLSessionConfiguration defaultSessionConfiguration];
//得到session對象
NSURLSession* session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// url
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
//創(chuàng)建任務
NSURLSessionDownloadTask* downloadTask = [session downloadTaskWithURL:url];
//開始任務
[downloadTask resume];
}
#pragma mark -- NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location{
NSError *saveError;
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *savePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.amr",downloadFileName]];
NSURL *saveUrl = [NSURL fileURLWithPath:savePath];
//把下載的內容從cache復制到document下
[[NSFileManager defaultManager] copyItemAtURL:location toURL:saveUrl error:&saveError];
if (!saveError) {
NSLog(@"save success");
}else{
NSLog(@"save error:%@",saveError.localizedDescription);
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
NSLog(@"%@",[NSString stringWithFormat:@"下載進度:%f",(double)totalBytesWritten/totalBytesExpectedToWrite]);
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes{
}