美文网首页
ios 断点离线下载总结

ios 断点离线下载总结

作者: 八匹马_BPM | 来源:发表于2019-03-01 12:25 被阅读0次

    使用NSURLSessionDownloadTask来下载任务(可以实现断点下载,离线下载还是使用NSURLSessionDataTask)

    NSURLSessionDownloadTask断点下载 遵守NSURLSessionDownloadDelegate

    #pragma mark 断点下载 不需要把每段下载的数据保存到本地 只需要把下载完成的数据保存就可以了 断点下载是通过resumeData(cancelByProducingResumeData保存下载到哪里了)然后downloadTaskWithResumeData继续从上次保存的位置开始下载 app在后台下载和app没关系(系统调度的另一个进程在进行后台下载)

    -(void)dataDownLoadTask:(UIButton*)sender{

        // 按钮状态取反

        sender.selected= !sender.isSelected;

        if (nil == self.downloadTask) { // [开始下载/继续下载]

            if (self.resumeData) { // [继续下载]

                // 传入上次暂停下载返回的数据,就可以恢复下载

                self.downloadTask = [self.session downloadTaskWithResumeData:self.resumeData];

                // 开始任务

                [self.downloadTask resume];

                self.resumeData = nil;

            }else{ // [开始下载]:从0开始下载

                NSURL* url = [NSURL URLWithString:@"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg"];

                // 创建任务

                self.downloadTask = [self.session downloadTaskWithURL:url];

                // 开始任务

                [self.downloadTask resume];

            }

        }else{ // [暂停下载]

            __weaktypeof(self) weakSelf =self;

            [self.downloadTask cancelByProducingResumeData:^(NSData *resumeData) {

                // resumeData:包含了继续下载的位置\下载的路径

                weakSelf.resumeData= resumeData;

                weakSelf.downloadTask=nil;

            }];

        }

    }

    代理回调方法

    /**

     *  每次写入数据到临时文件时,就会调用一次这个方法。可在这里获得下载进度

     *

     *  @parambytesWritten              这次写入的文件大小

     *  @paramtotalBytesWritten        已经写入沙盒的文件大小

     *  @paramtotalBytesExpectedToWrite 文件总大小

     */

    - (void)URLSession:(NSURLSession*)session downloadTask:(NSURLSessionDownloadTask*)downloadTask

          didWriteData:(int64_t)bytesWritten

     totalBytesWritten:(int64_t)totalBytesWritten

    totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

    {

        NSLog(@"读取字节大小%lld,当前线程%@,%@",bytesWritten,[NSThread currentThread],NSHomeDirectory());

        // 下载进度

        dispatch_async(dispatch_get_main_queue(), ^{

            self.progress.progress=1.0* totalBytesWritten / totalBytesExpectedToWrite;

            self.progressLabel.text= [NSStringstringWithFormat:@"当前下载进度:%.2f%%",100.0* totalBytesWritten / totalBytesExpectedToWrite];

        });

    }

    /**

     *  恢复下载后调用 不常在这里面操作

     */

    - (void)URLSession:(NSURLSession*)session downloadTask:(NSURLSessionDownloadTask*)downloadTask

     didResumeAtOffset:(int64_t)fileOffset

    expectedTotalBytes:(int64_t)expectedTotalBytes

    {

    }

    /**

     *  文件下载完毕时调用

     */

    - (void)URLSession:(NSURLSession*)session downloadTask:(NSURLSessionDownloadTask*)downloadTask

    didFinishDownloadingToURL:(NSURL*)location

    {

        NSLog(@"当前线程%@",[NSThread currentThread]);

        // 文件将要移动到的指定目录

        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

        // 新文件路径

        NSString *newFilePath = [documentsPath stringByAppendingPathComponent:@"QQ_V5.4.0.dmg"];

        NSLog(@"File downloaded to: %@",newFilePath);

        // 移动文件到新路径

        [[NSFileManager defaultManager] moveItemAtPath:location.path toPath:newFilePath error:nil];

    }

    这样就可以了

    NSURLSessionDataTask离线下载 遵守NSURLSessionDataDelegate

    /**

     * offlineDownLoadTask的懒加载,这里设置请求头中的Range

     */

    - (NSURLSessionDataTask*)offlineDownLoadTask {

        if (!_offlineDownLoadTask) {

            // 创建下载URL

            NSURL *url = [NSURL URLWithString:@"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg"];

            // 2.创建request请求

            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

            // 设置HTTP请求头中的Range    实现断点下载的关键

            NSString *range = [NSString stringWithFormat:@"bytes=%zd-", self.currentLength];

            [requestsetValue:range forHTTPHeaderField:@"Range"];

            // 3. 下载

            _offlineDownLoadTask = [self.session dataTaskWithRequest:request];

        }

        return _offlineDownLoadTask;

    }

    //离线下载  离线下载需要把每段下载的数据保存到本地 这样filehandler就可以操作文件的开始读写的位置  实现的核心部分是 [NSFileHandle fileHandleForWritingAtPath:path]对每次接受到的数据写入到沙盒 [self.fileHandle seekToEndOfFile] [self.fileHandle writeData:data]移动指针到文件结尾 然后在文件结尾继续拼接数据 这样就实现了重启app 后台退出app能够继续从沙盒中的位置开始下载(也叫离线下载)  app在后台下载和app没关系(系统调度的另一个进程在进行后台下载)

    -(void)dataDownLoadTask:(UIButton*)sender{

        // 按钮状态取反

        sender.selected= !sender.isSelected;

        if (sender.selected) { // [开始下载/继续下载]

            // 沙盒文件路径

            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"QQ_V5.4.0.dmg"];

            NSIntegercurrentLength = [selffileLengthForPath:path];

            if(currentLength >0) {  // [继续下载]

                self.currentLength= currentLength;

            }

            [self.offlineDownLoadTask resume];

        }else{

            //暂停

    //        [self.offlineDownLoadTask suspend];

            [self.offlineDownLoadTask cancel];

            self.offlineDownLoadTask = nil;

        }

    }

    /**

     * 获取已下载的文件大小

     */

    - (NSInteger)fileLengthForPath:(NSString*)path {

        NSIntegerfileLength =0;

        NSFileManager *fileManager = [NSFileManager defaultManager]; // default is not thread safe

        if([fileManagerfileExistsAtPath:path]) {

            NSError*error =nil;

            NSDictionary*fileDict = [fileManagerattributesOfItemAtPath:patherror:&error];

            if(!error && fileDict) {

                fileLength = [fileDictfileSize];

            }

        }

        returnfileLength;

    }

    代理回调

    /**

     * 接收到响应的时候:创建一个空的沙盒文件

     */

    - (void)URLSession:(NSURLSession*)session dataTask:(NSURLSessionDataTask*)dataTask didReceiveResponse:(NSURLResponse*)response completionHandler:(void(^)(NSURLSessionResponseDisposition))completionHandler

    {

        NSLog(@"下载长度%lld",response.expectedContentLength);

        // 获得下载文件的总长度:请求下载的文件长度 + 当前已经下载的文件长度

        self.fileLength = response.expectedContentLength + self.currentLength;

        // 沙盒文件路径

        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"QQ_V5.4.0.dmg"];

        NSLog(@"File downloaded to: %@",path);

        // 创建一个空的文件到沙盒中

        NSFileManager *manager = [NSFileManager defaultManager];

        if(![managerfileExistsAtPath:path]) {

            // 如果没有下载文件的话,就创建一个文件。如果有下载文件的话,则不用重新创建(不然会覆盖掉之前的文件)

            [managercreateFileAtPath:path contents:nil attributes:nil];

        }

        // 创建文件句柄

        self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];

        // 允许处理服务器的响应,才会继续接收服务器返回的数据

        completionHandler(NSURLSessionResponseAllow);

    }

    /**

     * 接收到具体数据:把数据写入沙盒文件中

     */

    - (void)URLSession:(NSURLSession*)session dataTask:(NSURLSessionDataTask*)dataTask didReceiveData:(NSData*)data

    {

        NSLog(@"当前线程%@",[NSThread currentThread]);

        // 指定数据的写入位置 -- 文件内容的最后面

        [self.fileHandle seekToEndOfFile];

        // 向沙盒写入数据

        [self.fileHandle writeData:data];

        // 拼接文件总长度

        self.currentLength += data.length;

        NSLog(@"%ld",self.currentLength);

        // 下载进度

        self.progress.progress=  1.0 * self.currentLength / self.fileLength;

        self.progressLabel.text = [NSString stringWithFormat:@"当前下载进度:%.2f%%",100.0 * self.currentLength / self.fileLength];

    }

    /**

     *  下载完文件之后调用:关闭文件、清空长度 (暂停suspend操作不会走该方法,取消cancel操作会走该方法)

     */

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

    {

        // 关闭fileHandle

        [self.fileHandle closeFile];

        self.fileHandle = nil;

        // 清空长度

        self.currentLength = 0;

        self.fileLength = 0;

    }

    个人github地址:zhuweigea (LaoZhu) · GitHub

    相关文章

      网友评论

          本文标题:ios 断点离线下载总结

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