ios 中用afnetworking下载word文档然后存储到沙盒library(acche)目录下怎么做,求大神指点

兴天虹兰 2017-04-18 02:22:10
ios 中用afnetworking下载word文档然后存储到沙盒library(acche)目录下怎么做,求大神指点
...全文
773 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39627853 2017-07-26
  • 打赏
  • 举报
回复
这里有AFNetworking解析 http://sep9.cn/3j532d
浅浅青丘 2017-07-15
  • 打赏
  • 举报
回复
//下载
/*
 url:资源下载地址
 saveFilePath:下载完存储的地址
 */
+ (void)downloadFile:(NSString *)url
         andSaveFile:(NSString *)saveFilePath
      andFinishBlock:(void (^)(BOOL success))finishCallback
    andProgressBlock:(void (^)(long long totalBytesWritten, long long totalBytesExpectedToWrite))progressCallback {
    
    NSURL *url1 = [[NSURL alloc] initWithString:url];
    //默认配置
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    //AFN3.0+基于封住URLSession的句柄
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSURLRequest *request = [NSURLRequest requestWithURL:url1];
    
    NSURLSessionDownloadTask *operation = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        //打印下下载进度
        NSLog(@"%lf",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
        if (progressCallback) {
            progressCallback(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount,downloadProgress.totalUnitCount);
        }
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        //下载地址
        NSLog(@"默认下载地址:%@",targetPath);
        
        //设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        return [NSURL URLWithString:filePath];
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        //下载完成后调用的方法
        NSLog(@"%@",response.URL);
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if (![fileManager fileExistsAtPath:saveFilePath]) {
            [fileManager createFileAtPath:saveFilePath contents:nil attributes:nil];
        }
        NSData * data = [NSData dataWithContentsOfURL:response.URL];
        if (data != nil) {
            [data writeToFile:saveFilePath atomically:NO];
        }
        
        if (finishCallback) {
            if (error == nil) {
                finishCallback(YES);
            }else{
                finishCallback(NO);
            }
            
        }
    }];
    [operation resume];
}

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧