c语言网络编程断点续传,网络编程(三) 下载任务,支持断点续传(示例代码)...

实现效果图:

23127121c86e41e39d1dfa0788779049.jpg

注意点:1.下载的文件会保存在沙盒目录下的tmp文件夹内,这个文件夹专门存储一些临时文件,我们需要在下载完成后把文件移动到自己需要的文件夹内,并修改文件的扩展名

2.有部分链接使用下载时resume data = nil;这样断点续传功能就没有用了;

在storyboard里面添加button,progressView和显示进度的label,并进行连线

ViewController.m代码

#import "ViewController.h"

@interface ViewController ()<

NSURLSessionDownloadDelegate

>

{

NSURLSessionDownloadTask *downTask;

NSData *myResumeData;

NSURLSession *session;

NSString *fielName;

}

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@property (weak, nonatomic) IBOutlet UILabel *progressLabel;

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

}

//创建下载任务

- (IBAction)downloadAction:(id)sender

{

if (downTask)

{

return;

}

//构造需要下载的url链接

NSURL *url = [NSURL URLWithString:@"http://apk500.bce.baidu-mgame.com/game/883000/883227/20160127061354_oem_5004211.apk?r=1"];

//设置保存文件名为url的最后部分

fielName = [url lastPathComponent];

//设置session工作类型为默认

NSURLSessionConfiguration *sessionCf = [NSURLSessionConfiguration defaultSessionConfiguration];

//用sessionConfig配置session

session = [NSURLSession sessionWithConfiguration:sessionCf delegate:self delegateQueue:[NSOperationQueue mainQueue]];

//设置任务类型

downTask = [session downloadTaskWithURL:url];

//发送任务请求

[downTask resume];

}

//暂停下载任务

- (IBAction)pauseAction:(UIButton *)sender

{

if (downTask.state == NSURLSessionTaskStateRunning)

{

[downTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {

//If resume data cannot be created, the completion handler will be called with nil resumeData

myResumeData = resumeData;

downTask = nil;

}];

}

}

//恢复下载任务

- (IBAction)resumeAction:(id)sender

{

if (myResumeData)

{

downTask = [session downloadTaskWithResumeData:myResumeData];

[downTask resume];

myResumeData = nil;

}

}

//把下载来的文件移动到Documents文件夹下并修改扩展名

- (void)moveFileToDocuments:(NSURL *)location

{

NSFileManager *manager = [NSFileManager defaultManager];

NSString *newPath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fielName]];

fielName = nil;

NSLog(@"%@",newPath);

[manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:newPath] error:nil];

}

#pragma mark - NSURLSessionDownloadDelegate

//在下载完成后调用

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

{

NSLog(@"%@",location);

[self moveFileToDocuments:location];

}

//进度条相关设置

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

{

float progress = (float)totalBytesWritten / totalBytesExpectedToWrite;

self.progressView.progress = progress;

self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress* 100];

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值