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

在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];


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

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值