美文网首页
断点下载

断点下载

作者: 海笙樾 | 来源:发表于2018-03-28 14:06 被阅读0次

今天做了断点下载视频或者MP3,具体实现如下

1、主要应用了NSURLSessionDownloadTask和NSURLSession并且遵循NSURLSessionDownloadDelegate

2、在沙盒目录下创建文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *file = [NSString stringWithFormat:@"%@/%@",_fileName,_sectionName];

    NSString *cachesPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:file];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:cachesPath];

if (blHave) {

}else{

if (![fileManager fileExistsAtPath:cachesPath]) {

            [fileManager createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];

}

  }

3、初始化下载方法

@property (nonatomic, strong) NSURLSessionDownloadTask * downloadTask;

@property (nonatomic, strong) NSURLSession *session;

NSURL * url = [NSURL URLWithString:_mainUrl];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //3.创建session

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

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

    //4.创建Task

    _downloadTask = [self.session downloadTaskWithRequest:request];

    //5.执行Task

    [_downloadTask resume];

5、实现代理

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

{

    //1. 获得文件的下载进度

    NSLog(@"%f",1.0 * totalBytesWritten/totalBytesExpectedToWrite);

    [self.delegate progress:1.0 * totalBytesWritten/totalBytesExpectedToWrite];

}

/**

* 当恢复下载的时候调用该方法

*

* @param fileOffset    从什么地方下载

* @param expectedTotalBytes 文件的总大小

*/

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes

{

    NSLog(@"%s",__func__);

}

/**

* 当下载完成的时候调用

*

* @param location  文件的临时存储路径

*/

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

{

NSArray * typeArray = [_mainUrl componentsSeparatedByString:@"."];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//定义自己的文件最终位置

    NSString *file = [NSString stringWithFormat:@"%@/%@/%@.%@",_fileName,_sectionName,_sectionName,[typeArray lastObject]];

    NSString *cachesPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:file];

    NSLog(@"文件的最终下载路径 %@",cachesPath);

    [[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:cachesPath] error:nil];

}

/**

* 请求结束

*/

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

{

}

6、断点下载的信息在 [_downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {

}];里的object.resumData里

可以把该对象存储在本地,下次进来时候取出该对象接着下载

downloadTask = [self.session downloadTaskWithResumeData:_resumData];

#pragma mark - 暂停下载

- (void)pauseDolo{

    __weak typeof(self) object = self;

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

        object.resumData = resumeData;

        NSString * resuData = [NSString stringWithFormat:@"%@.Data",_fileName];

        [USERDEFALE setValue:resumeData forKey:resuData];

        object.downloadTask = nil;

    }];

}

#pragma mark - 取消下载

- (void)cancelDolo{

    NSString * resuData = [NSString stringWithFormat:@"%@.Data",_fileName];

    [USERDEFALE removeObjectForKey:resuData];

    [_downloadTask cancel];

}

相关文章

  • 基于Okhttp实现断点下载(续传)和分片下载

    断点下载/续传 断点下载是针对下载大文件需求的一种优化机制,可以从上次下载的断点处继续下载。断点续传原理也相同,只...

  • 断点下载

    效果图 断点下载的过程 这里是简单的一个下载,下一篇介绍列表的断点下载1.绘制UI图,并初始化控件2.创建实体类:...

  • 断点下载

    今天做了断点下载视频或者MP3,具体实现如下 1、主要应用了NSURLSessionDownloadTask和NS...

  • 断点下载

    链接转载;判断当前Service是否是重启的Servciehttp://blog.csdn.net/luyi325...

  • 断点下载

    http://www.jianshu.com/p/f65e32012f07

  • 断点下载

    断点下载重点 利用HTTP请求头的Range属性,就可以实现从指定位置开始下载表示头500个字节:Range: b...

  • 断点下载

    需要遵循NSURLConnectionDataDelegate协议/** 当前的长度/@property (no...

  • 断点下载

  • OC网络:NSURLSession-downloadTask的断

    NSURLSessionDownloadDelegate断点下载说明: 能实现断点下载,即在程序不退出的情况下:暂...

  • Android下载文件(一)下载进度&断点续传

    索引 Android下载文件(一)下载进度&断点续传 Android下载文件(二)多线程并发&断点续传(待续) A...

网友评论

      本文标题:断点下载

      本文链接:https://www.haomeiwen.com/subject/eiiycftx.html