博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS网络基础学习三:NSURLSession的Download下载任务和代理方法
阅读量:3730 次
发布时间:2019-05-22

本文共 2918 字,大约阅读时间需要 9 分钟。

文章目录

block下载任务

- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent *)event{ //会话 NSURLSession *session=[NSURLSession sharedSession]; //2.根据会话创建任务 NSURLSessionDownloadTask *downloadTask= [session downloadTaskWithURL:[NSURL URLWithString:IMAGE_URL] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { //location是下载的本地地址 NSLog(@"location=%@",location); UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:location]]; dispatch_async(dispatch_get_main_queue(), ^{ self.imagview.image=image; }); }]; //3.启动任务 [downloadTask resume];}

下载代理方法

#import "ViewController.h"#define MYURL @"http://vjs.zencdn.net/v/oceans.mp4"@interface ViewController ()
@property (nonatomic,strong)NSURLSessionDownloadTask *task;@property(nonatomic,strong) NSData *data;@property(nonatomic,strong) NSURLSession *session;@end@implementation ViewController //创建会话-(NSURLSession *)session{ if(!_session){ _session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]]; } return _session;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (IBAction)start:(UIButton *)sender { //根据会话创建下载任务 self.task=[self.session downloadTaskWithURL:[NSURL URLWithString:MYURL]]; //启动任务 [self.task resume];}- (IBAction)pause:(UIButton *)sender { // [self.task suspend];//可以恢复任务 //cancelByProducingResumeData任务无法恢复,如果还是想能恢复继续下载,则记录下当前下载的数据 [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) { //resumeData表示当前下载了多少数据 self.data=resumeData; }];}- (IBAction)resume:(UIButton *)sender { // [self.task resume]; //对应cancelByProducingResumeData的在暂停的基础上继续下载 self.task=[self.session downloadTaskWithResumeData:self.data]; [self.task resume];}#pragma mark -协议方法-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{}//下载进度/* bytesWritten 当前次下载的数据大小 totalBytesWritten 总共下载了多少数据 totalBytesExceptedToWrite */-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{ NSLog(@"--%f",1.0*totalBytesWritten/totalBytesExpectedToWrite); }//恢复任务时调用的方法- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{ }//下载之后 文件所在位置-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{ //location文件下载的路径}@end

转载地址:http://apwin.baihongyu.com/

你可能感兴趣的文章
7.I/O系统
查看>>
2.2处理机调度
查看>>
2.3进程同步
查看>>
2.3浮点数的表示与运算
查看>>
2.4死锁
查看>>
2.4算术逻辑单元(ALU)
查看>>
3.1.1内存管理概念
查看>>
矩阵论——向量空间
查看>>
吴恩达机器学习——数值评价指标
查看>>
矩阵论——正交矩阵&Gram-Schimidt正交化
查看>>
Python语法——实现nc文件可视化的基本模块
查看>>
Anaconda完全入门指南
查看>>
如何创建、添加虚拟环境
查看>>
Anaconda prompt和cmd的区别
查看>>
第一个nc文件的信息读取
查看>>
netCDF——nc文件读取
查看>>
使用BaseMap绘制地图它不香么
查看>>
MeteoInfo介绍
查看>>
Basemap系列教程:使用shapefiles绘制地图
查看>>
numpy生成掩码数组
查看>>