//
// ViewController.m
// AFNetWorking
//
// Created by hq on 16/4/18.
// Copyright © 2016年 hanqing. All rights reserved.
//
#import "ViewController.h"
#import <AFNetworking.h>
@interface ViewController ()
- (IBAction)start:(UIButton *)sender;
- (IBAction)pause:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIProgressView *pro;
@property(nonatomic,strong) NSURLSessionDownloadTask *task;
@end
@implementation ViewController
-(NSURLSessionDownloadTask *)task{
if (!_task) {
AFHTTPSessionManager *session=[AFHTTPSessionManager manager];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"]];
_task=[session downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//下载进度
NSLog(@"%@",downloadProgress);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.pro.progress=downloadProgress.fractionCompleted;
}];
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//下载到哪个文件夹
NSString *cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
NSString *fileName=[cachePath stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:fileName];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//下载完成了
NSLog(@"下载完成了 %@",filePath);
}];
}
return _task;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)start:(UIButton *)sender {
[self.task resume];
}
- (IBAction)pause:(UIButton *)sender {
[self.task suspend];
}
@end
AFHTTPSessionManager文件下载
最新推荐文章于 2018-02-17 21:37:22 发布
本文介绍了如何使用 AFNetworking 库实现视频下载的功能,包括创建下载任务、设置进度回调、下载到指定目录,并处理下载完成后的操作。
1330

被折叠的 条评论
为什么被折叠?



