iOS文件下载

小文件下载

方式一,使用NSData

//此方法相当于发送一个GET请求,直接将服务器的数据一次性下载下来
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *url = [NSURL URLWithString:@""];
        NSData *data = [NSData dataWithContentsOfURL:url];
    });

方式二,使用NSURLConnection

//此方法也是一次性将请求数据下载下来
NSURL *url = [NSURL URLWithString:@""];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
    }];

小文件下载的缺点:没法知道下载进度,并且使用data一次性将所有数据加载在内存中,如果data过大,就会出现内存益处

大文件下载

1.使用NSURLConnection

1>开始(继续)下载或者暂停

- (void)download:(UIButton *)button{
    button.selected = !button.isSelected;
    if (button.isSelected) {//开始下载或者继续下载
        NSURL *url = [NSURL URLWithString:@""];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        //设置请求头(断点下载必须设置)
        //设置请求头的Range,使用bytes=100-(从100字节开始下载后面所有数据)
        NSString *range = [NSString stringWithFormat:@"bytes=%lld-",self.currentLength];
        [request setValue:range forHTTPHeaderField:@"Range"];
        //创建一个urlConnection并且开始发送异步请求
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
    }else{//暂停
        [self.connection cancel];
        self.connection = nil;
    }
}

2>接受到服务器响应

#pragma mark -- NSURLConnectionDataDelegate

/**
 接受到服务器的响应就会调用此方法

 @param response 响应
 */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    //如果已经有下载进度,就没必要创建文件
    if (self.currentLength) {
        return;
    }
//    NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
//    long long taotalFileLenth = [resp.allHeaderFields[@"Content-Length"] longLongValue];
//    获取要下载文件的总大小,相等于上面的方法
    self.totoalFileLenth = response.expectedContentLength;
    
//  收到响应后在沙盒路经创建一个本地文件
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//    根据请求路经的文件名创建本地的文件名
    NSString *filePath = [caches stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",connection.currentRequest.URL.absoluteString.lastPathComponent]];
//    创建一个空的文件到沙盒中
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
//    创建一个用来写数据的文件句柄来操作数据写入
    self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
}

3>接受到具体数据

/**
 接收到服务器返回的实体数据时调用
 此方法可能会被调用多次

 @param data 本次返回的数据
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//    将文件句柄移动到文件的最后面
    [self.writeHandle seekToEndOfFile];
//    将接受到的数据写入本地文件中
    [self.writeHandle writeData:data];
    
//    累计文件的长度
    self.currentLength += data.length;
    NSLog(@"下载进度: %f",(double)self.currentLength / self.totoalFileLenth);
}

4>所有数据下载完成

/**
 所有数据接受完毕调用
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//    下载完毕后清空当前进度和文件总长度
    self.currentLength = 0;
    self.totoalFileLenth = 0;
//    关闭fileHandle
    [self.writeHandle closeFile];
    self.writeHandle = nil;
}

5>网络链接失败

/**
 链接失败时会调用
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    
}

这里只是一点思路,如果要做完成的断点下载还必须处理网络变化,app前后台切换等。(本地保存下载进度就可以处理所有逻辑,这里不做介绍)

2.NSURLSession

NSURLSession是iOS7推出的用来代替NSURLConnection,NSURLSession的使用方法
1>获取NSURLSession对象
2>利用NSURLSession对象创建对应的任务(Task)
3>开始任务([Task resume])
使用NSURL实现下载任务的方法
1>一次性下载

NSURL *url = [NSURL URLWithString:@""];
//1.获取session
NSURLSession *session = [NSURLSession sharedSession];
//使用session创建一个下载任务
NSURLSessionDownloadTask *downLoadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  //当所有数据下载完成后会调用此block
  //location为下载完成后文件保存的硬盘的临时路径(Temp文件夹下面),如果不处理就会马上删除,一般是将下载好的文件剪切到caches路径下面保存起来
  //此下载方法是边下边写,不会占用太多内存,缺点是只有当所有数据都下载下来后才能调用此block,所有没法知道下载进度
}];
//开始任务
[downLoadTask resume];

2>断点下载

//1.获取NSURLSession
- (NSURLSession *)session{
    if (!_session) {
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    }
    return _session;
}

//2.开始下载任务
- (void)start{
    NSURL *url = [NSURL URLWithString:@""];
    self.task = [self.session downloadTaskWithURL:url];
    [self.task resume];
}

//3.暂停
- (void)pause{
    __weak typeof(self) weakSelf = self;
    //resumeData包含下载的路径和继续下载的位置
    [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
        weakSelf.resumeData = resumeData;
        weakSelf.task = nil;
    }];
}

//4。恢复下载
- (void)resume{
    self.task = [self.session downloadTaskWithResumeData:self.resumeData];
    [self.task resume];
    self.resumeData = nil;
}

#pragma mark -- NSURLSessionDownloadDelegate
/**
 下载数据写入本地(可能会调用多次)

 @param bytesWritten 本次写入数据大小
 @param totalBytesWritten 已经写入数据大小
 @param totalBytesExpectedToWrite 总共需要写入数据大小
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    //获取下载进度
    double pregress = (double)totalBytesWritten / totalBytesExpectedToWrite;
}

/**
 恢复下载
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{
    
}

/**
 下载完成调用

 @param location 写入本地临时路经(temp文件夹里面)
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //获取服务器的文件名
    NSString *fileName = downloadTask.response.suggestedFilename;
    //创建需要保存在本地的文件路径
    NSString *filePath = [caches stringByAppendingPathComponent:fileName];
    //将下载的文件剪切到上面的路径
    [[NSFileManager defaultManager] moveItemAtPath:location.path toPath:filePath error:nil];
}

你可能感兴趣的:(iOS文件下载)