搬砖iOS开发进阶

iOS 下载一个文件

2016-09-28  本文已影响2553人  MrSYLong

方法一:可以直接下载一个文件


- (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];

//创建任务

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{

}

上一篇 下一篇

猜你喜欢

热点阅读