- (IBAction)downLoad:(id)sender {
//下载地址
NSString *urlString = @"http://218.76.27.57:8080/chinaschool_rs02/135275/153903/160861/160867/1370744550357.mp3";
//创建manager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//创建请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
//创建下载任务
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%lli/%lli", downloadProgress.completedUnitCount, downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//接受到响应头,需要制定保存路径
NSLog(@"状态码:%li", ((NSHTTPURLResponse *)response).statusCode);
//返回值 指定的保存路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/菊花台.mp3"];
NSLog(@"%@", filePath);
//创建一个沙盒路径下的子路径,保存文件
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if(error){
NSLog(@"下载失败");
}else {
NSLog(@"下载完成");
}
}];
//发起下载任务
[downloadTask resume];
AFNetworking实现下载
最新推荐文章于 2023-09-04 21:56:50 发布