iOS-网络组件(下载任务演示)
2016-08-14 本文已影响41人
如风家的秘密
故事板的绘图:注意各方法与代码关联
代码实现:
#import "ViewController.h"
@interface ViewController ()<NSURLSessionDownloadDelegate>
{
NSURLSessionDownloadTask *task1;
NSURLSession *session1;
NSData *saveData;//已经下载的数据
}
@property (strong, nonatomic) IBOutlet UIProgressView *myProgressVIew;
@end
@implementation ViewController
//普通下载任务
- (IBAction)downLoadTask:(id)sender {
//1 url
NSURL *url = [NSURL URLWithString:@"http://www.pptbz.com/pptpic/UploadFiles_6909/201204/2012041411433867.jpg"];
//2 request
//3 session
NSURLSession *session = [NSURLSession sharedSession];
//4 task
NSURLSessionDownloadTask *task =[session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//location:下载的文件的临时保存的位置(tmp),当block调用完会被删除掉
NSLog(@"location = %@",location);
NSLog(@"response = %@",response);//响应头
//移动文件
NSFileManager *manager = [NSFileManager defaultManager];
NSString *newPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/111.jpg"];
NSURL *newUrl = [NSURL fileURLWithPath:newPath];
[manager moveItemAtURL:location toURL:newUrl error:nil];
NSLog(@"newsPath = %@",newPath);
}];
//5 resume
[task resume];
}
//开启下载
- (IBAction)beginDownLoad:(id)sender {
//1 url
// NSURL *url = [NSURL URLWithString:@"http://www.pptbz.com/pptpic/UploadFiles_6909/201204/2012041411433867.jpg"];
// NSURL *url = [NSURL URLWithString:@"http://wl.baidu190.com/1470730340/20160879819642dc26a12e0bfc0dc55fda98d5.mp3"];
NSURL *url = [NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];
//2 request
//3 session
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
session1 = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
//4 task
task1 =[session1 downloadTaskWithURL:url];
//5 resume
[task1 resume];
}
//暂停下载
- (IBAction)stopDownLoad:(id)sender {
//1任务取消,所有的数据删除
// [task cancel];
//2 暂停
[task1 cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
NSLog(@"resumeData = %@",resumeData);//resumeData:已经下载好的数据
saveData = resumeData;
//取消任务之后,把之前的任务丢弃掉
task1 = nil;//安全释放
}];
}
//继续下载
- (IBAction)continueDownLoad:(id)sender {
//重新开始下载任务
task1 = [session1 downloadTaskWithResumeData:saveData];
//重新开启任务
[task1 resume]; //resume:恢复 下载任务
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - NSURLdownload Delegate
//已经下载完成后
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location{
//移动文件
NSFileManager *manager = [NSFileManager defaultManager];
NSString *newPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/222.mp4"];
NSURL *newUrl = [NSURL fileURLWithPath:newPath];
[manager moveItemAtURL:location toURL:newUrl error:nil];
NSLog(@"newsPath = %@",newPath);
}
// 数据在传输的过程中,不断调用的方法(每传输一个数据包,就会调用一次)
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
/*
bytesWritten:本次下载传输的字节
totalBytesWritten:已经下载的字节
totalBytesExpectedToWrite:文件的总大小
*/
NSLog(@"bytesWritten=%lld totalBytesWritten= %lld totalBytesExpectedToWrite= %lld",bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);
//进度
float progress =totalBytesWritten/(float)totalBytesExpectedToWrite;
self.myProgressVIew.progress = progress;
}
@end