博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AFNetworking 下载文件断点续传操作
阅读量:5740 次
发布时间:2019-06-18

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

一:本示例代码包括:

  • 文件下载,写入指定目录
  • 下载进度,回调Progress;
  • 断点续传,下载暂停,继续操作;

二:本项目 适用于 AFNetworking 1.x 版本

1 #pragma mark 断点续传下载; 2      3     //断点下载续传--需要服务器端的支持,本次测试为下载百度音乐,此接口,会在第二次暂停,继续下载时,出404错误; 4      5     //进度 6     UIProgressView  *downProgressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault]; 7     downProgressView.center = CGPointMake(self.view.center.x, 20); 8     downProgressView.progress = 0; 9     downProgressView.progressTintColor = [UIColor blueColor];10     downProgressView.trackTintColor = [UIColor grayColor];11     [self.view addSubview:downProgressView];12     13     //开始14     UIButton *start = [UIButton buttonWithType:UIButtonTypeRoundedRect];15     start.frame = CGRectMake(25, 40, 80, 30);16     [start setTitle:@"开始" forState:UIControlStateNormal];17     [start addTarget:self action:@selector(startDownloadOp) forControlEvents:UIControlEventTouchUpInside];18     19     //暂停20     UIButton *pause = [UIButton buttonWithType:UIButtonTypeRoundedRect];21     pause.frame = CGRectMake(125, 40, 80, 30);22     [pause setTitle:@"暂停" forState:UIControlStateNormal];23     [pause addTarget:self action:@selector(pausetDownloadOp) forControlEvents:UIControlEventTouchUpInside];24     25     //继续26     UIButton *resume = [UIButton buttonWithType:UIButtonTypeRoundedRect];27     resume.frame = CGRectMake(220, 40, 80, 30);28     [resume setTitle:@"继续" forState:UIControlStateNormal];29     [resume addTarget:self action:@selector(resumeDownloadOp) forControlEvents:UIControlEventTouchUpInside];30     31     [self.view addSubview:start];32     [self.view addSubview:pause];33     [self.view addSubview:resume];34     35     //文件名36     NSString *fileName = [NSString stringWithFormat:@"%@愿得一人听.mp3",NSTemporaryDirectory()];37     NSLog(@"%@",fileName);38     39     //下载40     NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:MusicOne]];41     self.downloadMusicOP = [[AFHTTPRequestOperation alloc]initWithRequest:downloadRequest];42     _downloadMusicOP.outputStream = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];43     44     [_downloadMusicOP setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {45         CGFloat progress = ((float)totalBytesRead) / totalBytesExpectedToRead;46         [downProgressView setProgress:progress animated:YES];47         start.enabled = NO;48         [start setTitle:@"下载中" forState:UIControlStateNormal];49     }];50     51     [_downloadMusicOP setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {52         start.enabled = NO;53         [start setTitle:@"完成" forState:UIControlStateNormal];54         NSLog(@"download finish");55         NSLog(@"%@",[[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:nil]);56         57     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {58         NSLog(@"error %@",error);59     }];60     61         62 63     64     65 }66 67 #pragma mark AFNetworking 下载之断点续传;68 69 - (void)startDownloadOp70 {71     [_downloadMusicOP start];72 }73 74 - (void)pausetDownloadOp75 {76     [_downloadMusicOP pause];77 }78 79 - (void)resumeDownloadOp80 {81     [_downloadMusicOP resume];82 }

   项目代码:

 

转载于:https://www.cnblogs.com/cocoajin/p/3484487.html

你可能感兴趣的文章
“亲切照料”下的领域驱动设计
查看>>
SRE工程师到底是做什么的?
查看>>
解读:Red Hat为什么收购Ansible
查看>>
PHP json_encode() 函数介绍
查看>>
js动态设置元素高度
查看>>
Ossim下的安全合规管理
查看>>
DelphiWebMVC框架下BPL热部署实现
查看>>
C++与MySQL的冲突
查看>>
siki学习之观察者模式笔记
查看>>
单元测试
查看>>
spring.net 继承
查看>>
ES6:模块简单解释
查看>>
JavaScript indexOf() 方法
查看>>
用Bootstrap写一份简历
查看>>
ZJU PAT 1023
查看>>
WMI远程访问问题解决方法
查看>>
从零开始学习IOS,(UILabel控件)详细使用和特殊效果
查看>>
Android开发历程_15(AppWidget的使用)
查看>>
阿花宝宝 Java 笔记 之 初识java
查看>>
7、设计模式-创建型模式-建造者模式
查看>>