[[AFHTTPSessionManager manager] downloadTaskWithRequest:request下载MP3 MP4 进度
2018-01-23 16:14
841 查看
//引入文件
//#import "AFNetworking.h"
//#import "UIProgressView+AFNetworking.h"
AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObjects:@"application/json",@"text/json",
@"text/javascript",@"text/html",@"video/mpeg",@"video/mp4",@"audio/mp3",nil];//可下载@"text/json", @"text/javascript",@"text/html",@"video/mpeg",@"video/mp4",@"audio/mp3"等
manager.responseSerializer = [AFHTTPResponseSerializerserializer];
NSString *fullPath = fileName;//要保存的沙盒路径
NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:url]];//在线路径
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request
progress:^(NSProgress *downloadProgress) {
[[NSOperationQueuemainQueue]
addOperationWithBlock:^{ //下载过程中由多个线程返回downloadProgress,无法给progress赋值进度,所以要选出主线程
downloadView.observedProgress = downloadProgress;
}];
} destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {
NSString *path_sandox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)[0];
NSLog(@"path_sandox:%@",path_sandox);
NSString *path = [path_sandox
stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"path:%@",path);
return [NSURLfileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse *_Nonnull response,
NSURL * _Nullable filePath,
NSError * _Nullable error) {
if(error){
NSLog(@"Erroryue: %@", error);
[ProgressHUD
showError:@"下载失败"];
}else{
NSLog(@"success 下载完成");
}
[downloadViewremoveFromSuperview];
}];
[task resume];
[downloadViewsetProgressWithDownloadProgressOfTask:task
animated:YES];//downloadView是显示进度的进度条
//
}
//#import "AFNetworking.h"
//#import "UIProgressView+AFNetworking.h"
AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObjects:@"application/json",@"text/json",
@"text/javascript",@"text/html",@"video/mpeg",@"video/mp4",@"audio/mp3",nil];//可下载@"text/json", @"text/javascript",@"text/html",@"video/mpeg",@"video/mp4",@"audio/mp3"等
manager.responseSerializer = [AFHTTPResponseSerializerserializer];
NSString *fullPath = fileName;//要保存的沙盒路径
NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:url]];//在线路径
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request
progress:^(NSProgress *downloadProgress) {
[[NSOperationQueuemainQueue]
addOperationWithBlock:^{ //下载过程中由多个线程返回downloadProgress,无法给progress赋值进度,所以要选出主线程
downloadView.observedProgress = downloadProgress;
}];
} destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {
NSString *path_sandox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)[0];
NSLog(@"path_sandox:%@",path_sandox);
NSString *path = [path_sandox
stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"path:%@",path);
return [NSURLfileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse *_Nonnull response,
NSURL * _Nullable filePath,
NSError * _Nullable error) {
if(error){
NSLog(@"Erroryue: %@", error);
[ProgressHUD
showError:@"下载失败"];
}else{
NSLog(@"success 下载完成");
}
[downloadViewremoveFromSuperview];
}];
[task resume];
[downloadViewsetProgressWithDownloadProgressOfTask:task
animated:YES];//downloadView是显示进度的进度条
//
}
相关文章推荐
- 通过AFHTTPSessionManager+RACSupport 或 AFHTTPRequestOperationManager+RACSupport 解决同一信号源(RAC网络请求两次的问题
- 使用NSURLSessionDownloadTask实现大文件下载-监听下载进度
- 【iOS开发-网络】使用NSURLSessionDownloadTask下载任务(不能知道下载进度)
- 【iOS开发-网络】使用NSURLSessionDownloadTask下载任务(有下载进度)
- AFHTTPSessionManager进行download
- 网络篇-NSURLSessionDownloadTask进度下载(续上节)
- AFHTTPSessionManager文件下载
- AFNetworking源码 - AFHTTPSessionManager
- ASIHttpRequest下载服务端的mp3...文件
- iOS开发网络篇 一一 NSURLSessionDownloadTask实现断点下载
- NSURLSessionDownloadTask(下载任务)
- AFNetworking 3.0 源码解读(六)之 AFHTTPSessionManager
- 使用 new XMLHttpRequest() 制作下载文件进度条
- NSURLSessionDownloadTask实现大文件下载
- iOS_NSURLSessionDownloadTask_显示加载进度_断点续传(网络中断情况).
- AFNetworking 3.0 源码解读(六)之 AFHTTPSessionManager
- ios 后台下载,app退出再进入可以断点续传 NSURLSessionDownloadTask(一)
- 使用WebClient HttpWebRequest 的高级应用,获取带验证的session下载权限
- ios开发网络学习九:NSURLSessionDownloadTask实现大文件下载
- AFHTTPSessionManager获取cookie的方法