前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS开发中利用AFNetworking下载大文件以及下载文件的删除

iOS开发中利用AFNetworking下载大文件以及下载文件的删除

作者头像
用户1451823
发布2018-09-13 15:29:06
3.2K0
发布2018-09-13 15:29:06
举报
文章被收录于专栏:DannyHoo的专栏DannyHoo的专栏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1337686

在iOS开发的某些项目中有下载的功能,如视频的下载,本篇博客说的是利用AFNetworking进行下载。代码是我从网上找的,但网上的代码有一个问题,它将下载的视频存放到了沙盒的Document文件下,这样是不对的。Document文件不能存放大的文件和下载的东西,我们需要将下载的大文件存放到沙盒下的Library文件下的Caches文件下。直接上代码:

NSURLSessionConfiguration *configuration = NSURLSessionConfiguration defaultSessionConfiguration;

// 1. 创建会话管理者

AFURLSessionManager *manager = [AFURLSessionManager alloc initWithSessionConfiguration:configuration];

// 2. 创建下载路径和请求对象

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

NSURLRequest *request = NSURLRequest requestWithURL:URL;

// 3.创建下载任务

/**

     * 第一个参数 - request:请求对象

     * 第二个参数 - progress:下载进度block

     *      其中: downloadProgress.completedUnitCount:已经完成的大小

     *            downloadProgress.totalUnitCount:文件的总大小

     * 第三个参数 - destination:自动完成文件剪切操作

     *      其中: 返回值:该文件应该被剪切到哪里

     *            targetPath:临时路径 tmp NSURL

     *            response:响应头

     * 第四个参数 - completionHandler:下载完成回调

     *      其中: filePath:真实路径 == 第三个参数的返回值

     *            error:错误信息

     */

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress) {

__weak typeof(self) weakSelf = self;

// 获取主线程,不然无法正确显示进度。

NSOperationQueue* mainQueue = NSOperationQueue mainQueue;

        [mainQueue addOperationWithBlock:^{

// 下载进度

            weakSelf.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;

            weakSelf.progressLabel.text = NSString stringWithFormat:@"当前下载进度:%.2f%%",100.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;

        }];

    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

// 文件下载路径 我们下载的大文件如视频应该放在沙盒的Library文件下

NSString * caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject;

NSString * filePathStr = caches stringByAppendingString:@"/vv.dmg";

NSURL * filePath = NSURL URLWithString:filePathStr;

NSFileManager * fileManager = NSFileManager defaultManager;

// 创建一个空的文件

        fileManager createFileAtPath:filePathStr contents:nil attributes:nil;

//        NSURL *path = [NSFileManager defaultManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

//        NSURL * filePath = path URLByAppendingPathComponent:@"QQ_V5.4.0.dmg";

return filePath;

    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

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

NSString * fileStr = filePath.absoluteString;

NSLog(@"%@", fileStr);

_filePath = fileStr;

    }];

// 4. 开启下载任务

    downloadTask resume;

项目中有下载功能必然也有删除下载文件的功能,删除就比较简单了,我们只需利用NSFileManager这个类就可以实现删除下载的文件的功能。代码:

NSFileManager * fileManager = NSFileManager defaultManager;

// 删除文件

    fileManager removeItemAtPath:_filePath error:nil;

我们只要获取删除文件的路径即可。

本篇博客到此结束,谢谢查看!!!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年07月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档