ios 中用afnetworking下载word文档然后存储到沙盒library(acche)目录下怎么做,求大神指点 ios 中用afnetworking下载word文档然后存储到沙盒library(acche)目录下怎么做,求大神指点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //下载/* 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];} 这里有AFNetworking解析 http://sep9.cn/3j532d 求大牛赐教“实况2013的模块rld.dll已加载,但找不到入口点” asyncSocket发送图片 急!高分求解决IPHONE连接路由器就断网 ios 有关网络的一个问题 如何禁止UITextField的弹出菜单 Select / Select All的出现 iOS视频拍摄功能已经从手机中取视频文件夹 xcode6 为storyboard添加基于view类错误 有没有谁能帮我看一下这段代码,感觉没什么问题,但是并没有效果,谢谢 Jenkins 插件 SVN Publisher 上传文件的问题 去除tableView的cell缓存 大神看过来 iOS appstore 审核被拒.求助.
/*
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];
}