美文网首页
下载文件至本地缓存

下载文件至本地缓存

作者: JohnayXiao | 来源:发表于2019-12-31 16:40 被阅读0次
    - (void)download {
        AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
        NSString *url = @"网络文件地址";
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        
        NSURLSessionDownloadTask *loadTask = [manger downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
            //下载进度监听
            NSLog(@"Progress:----%.2f%",100.0*downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
            NSLog(@"fullPath:%@",fullPath);
            NSLog(@"targetPath:%@",targetPath);
            return [NSURL fileURLWithPath:fullPath];
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            NSLog(@"filePath:%@",filePath);
        }];
        [loadTask resume];
    }
    

    相关文章

      网友评论

          本文标题:下载文件至本地缓存

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