您的位置:首页 > 理论基础 > 计算机网络

[[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是显示进度的进度条

//

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息