iOS NSURLSessionDownloadTask设置代理文件下载的示例

所属分类: 软件编程 / IOS 阅读数: 619
收藏 0 赞 0 分享

通过设置代理我们可以拿到下载进度,对于大文件,我们还需要做到开始、暂停、继续以及取消等相应操作,这篇文章先简单的介绍一下通过代理来实现文件下载的问题:

#import "ViewController.h"
@interface ViewController ()<NSURLSessionDownloadDelegate>
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  [self delegate];
}

-(void)delegate
{
  //1.url
  NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_03.png"];
  
  //2.创建请求对象
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  
  //3.创建session :注意代理为NSURLSessionDownloadDelegate
  NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
  
  //4.创建Task
  NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
  
  //5.执行Task
  [downloadTask resume];
}

#pragma mark ----------------------
#pragma mark NSURLSessionDownloadDelegate
/**
 * 写数据
 *
 * @param session          会话对象
 * @param downloadTask       下载任务
 * @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
{
  //1. 获得文件的下载进度
  NSLog(@"%f",1.0 * totalBytesWritten/totalBytesExpectedToWrite);
}

/**
 * 当恢复下载的时候调用该方法
 *
 * @param fileOffset     从什么地方下载
 * @param expectedTotalBytes 文件的总大小
 */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
  NSLog(@"%s",__func__);
}

/**
 * 当下载完成的时候调用
 *
 * @param location   文件的临时存储路径
 */
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
  NSLog(@"%@",location);
  
  //1 拼接文件全路径
  NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
  
  //2 剪切文件
  [[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
  NSLog(@"%@",fullPath);
}

/**
 * 请求结束
 */
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
  NSLog(@"didCompleteWithError");
}
@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

iOS App中UILabel的自定义及在Auto Layout中的使用

这篇文章主要介绍了iOS App中UILabel的自定义及在Auto Layout中的使用,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS应用开发中UITableView的分割线的一些设置技巧

这篇文章主要介绍了iOS应用开发中UITableView分割线的一些设置技巧,包括消除分割线的方法,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

详解iOS时间选择框

这篇文章主要为大家详细介绍了详解iOS时间选择框,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解iOS App开发中UIViewController的loadView方法使用

这篇文章主要介绍了详解iOS App开发中UIViewController的loadView方法使用,讲解了访问view属性时loadView方法的调用及使用loadView时的一些注意点,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在iOS App中自定义和隐藏状态栏的方法

这篇文章主要介绍了在iOS App中自定义和隐藏状态栏的方法,在顶部时某些状况下即用应用内的状态栏覆盖系统本身的,代码示例为Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS开发实现音频播放功能

本文给大家分享的是在IOS开发过程中实现音频播放的功能,讲解的十分细致,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

IOS开发实现录音功能

本文给大家分享的是一个IOS开发中实现录音功能的实例,并简单给大家解析一下,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS实现图片轮播效果

这篇文章主要为大家详细介绍了IOS实现图片轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS通过http post上传图片

这篇文章主要介绍了iOS通过http post上传图片的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS框架Spring常用的动画效果

本文给大家介绍的是在IOS开发中常用的动画效果以及自定义转场动画特效的代码,非常的简单实用,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多