IOS 网络浅析-(十 NSURLSession下载简介)

之前本来打算在写两个篇幅,但是在这片开写的时候觉得还是写一个比较好,有利于理解。NSURLSession下载是通过NSURLSession下载代理实现的,上一片也介绍了代理,之所以没有介绍下载是因为,我个人觉得容易混淆(应该是我太笨)。这篇随笔里将会介绍NSURLSession下载的实现,包括下载的开始,挂起,继续。希望本片可以给大家带来点点收获。下一篇我打算介绍一下AFN毕竟实际开发中AFN才是硬道理。

 

//
//  ViewController.m
//  NSURLSession大文件下载
//
//  Created by 大欢 on 16/3/21.
//  Copyright © 2016年 大欢. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDownloadDelegate>

@property (nonatomic, strong) NSURLSessionDownloadTask * task;

@property (nonatomic, strong) NSData * resumeData;

@property (nonatomic, strong) NSURLSession * session;

@end

@implementation ViewController


- (IBAction)start:(id)sender {
    
    NSURLSession * session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
    
    self.session = session;

    self.task = [session downloadTaskWithURL:[NSURL URLWithString:[@"http://192.168.1.200/DOM解析.mp4"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    
    [self.task resume];
}


- (IBAction)pause:(id)sender {
    
    //暂停就是将任务挂起
    
    [self.task cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
        
        //保存已下载的数据
        self.resumeData = resumeData;
    }];
}

- (IBAction)resume:(id)sender {
    
    //可以使用ResumeData创建任务
    
    self.task = [self.session downloadTaskWithResumeData:self.resumeData];
    
    //开启继续下载
    [self.task resume];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSLog(@"%@",NSSearchPathForDirectoriesInDomains(9, 1, 1));
}


/* 
 
 监测临时文件下载的数据大小,当每次写入临时文件时,就会调用一次
 
 bytesWritten 单次写入多少
 totalBytesWritten  已经写入了多少
 totalBytesExpectedToWrite 文件总大小
 
 */

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
      didWriteData:(int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
    
    //打印下载百分比
    NSLog(@"%f",totalBytesWritten * 1.0 / totalBytesExpectedToWrite);
    
}

//下载完成

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {
    
    
    NSString * cachesPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
    
    NSFileManager * mgr = [NSFileManager defaultManager];
    
    [mgr moveItemAtURL:location toURL:[NSURL fileURLWithPath:cachesPath] error:NULL];
    
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
    
    NSLog(@"%@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

posted @ 2016-03-21 20:28  旭宝爱吃鱼  阅读(481)  评论(2编辑  收藏  举报