*未经博主允许不得擅自转载
*如转载请标明作者及出处
#import "ASIHTTPRequest.h" //ASI网络请求
#import "ASINetworkQueue.h"//ASI队列
@interface ViewController ()<ASIProgressDelegate>//此处代理为ProgressView(进度条签定)
@property (nonatomic, retain)ASIHTTPRequest *request;
@property (nonatomic, retain)ASINetworkQueue *netWorkQueue;
@property (nonatomic, retain)UIProgressView *progerssView;
@property (nonatomic, retain)UILabel *label;
@property (nonatomic, assign) BOOL select;
NSString *urlstring = @"http://hc25.aipai.com/user/656/20448656/6167672/card/25033081/card.mp4";
//初始化Documents路径
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//初始化临时文件路径
NSString *folderPath = [path stringByAppendingPathComponent:@"temp"];
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//判断temp文件夹是否存在
BOOL fileExists = [fileManager fileExistsAtPath:folderPath];
if (!fileExists) {//如果不存在说创建,因为下载时,不会自动创建文件夹
[fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
}
//设置下载路径
self.request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:urlstring]];
//设置ASIHTTPRequest代理
self.request.delegate = self;
// 设置文件路径下文件夹的名字
NSString *fileName = [urlstring lastPathComponent];
//初始化保存ZIP文件路径
NSString *Savepath = [path stringByAppendingPathComponent:fileName];
//初始化临时文件路径
NSString *tempPath = [path stringByAppendingPathComponent:@"linshi"];
//设置文件保存路径
[self.request setDownloadDestinationPath:Savepath];
//设置临时文件路径
[self.request setTemporaryFileDownloadPath:tempPath];
//设置进度条的代理,
[_request setDownloadProgressDelegate:self.progerssView];
// self.request.downloadProgressDelegate = self.progerssView;
//[self.request setShowAccurateProgress:YES];
//设置是是否支持断点下载
[self.request setAllowResumeForFileDownloads:YES];
//添加到ASINetworkQueue队列去下载
[self.netWorkQueue go];
[self.netWorkQueue addOperation:self.request];
[self.request setCompletionBlock:^{
NSLog(@"下载成功,文件保存到: %@",NSHomeDirectory());
}];
[self.request setFailedBlock:^{
NSLog(@"下载失败");
}];
- (void)creaeButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(downloade:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 180, 80, 30);
[button setTitle:@"下载" forState:UIControlStateNormal];
[self.view addSubview:button];
}
- (void)downloade:(UIButton *)sender {
if ((sender.selected =! sender.selected)) {
[sender setTitle:@"暂停" forState:UIControlStateNormal];
[self handleData];
} else {
[sender setTitle:@"下载" forState:UIControlStateNormal];
for (ASIHTTPRequest *request in [self.netWorkQueue operations]) {
//暂停的
[request clearDelegatesAndCancel];
}
}
}
- (void)createProgerssView {
self.progerssView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.progerssView.frame = CGRectMake(50, 100, 200, 20);
[self.view addSubview:self.progerssView];
[_progerssView release];
// 通过KVO监听 progress值,达到监听进图条的目的
[self.progerssView addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object change:(NSDictionary<NSString *,id> *)change
context:(void *)context {
NSNumber *value = [change objectForKey:@"new"];
float progress = [value floatValue];
NSLog(@"%.1f%%",progress *100);
NSString *pro = [NSString stringWithFormat:@"%.1f%%",progress *100];
self.label.text = pro;
}
- 9.这是我们在ViewDidLoad方法中进行调用
- (void)viewDidLoad {
[super viewDidLoad];
//ASINetworkQueue是NSOperationQueue子类,
//iPhone 提供了 NSOperation 接口进行任务对象的封装,而通过将任务对象加入到 NSOperationQueue 队列.
//NSOperationQueue 队列会分配线程进行任务对象的执行.
self.netWorkQueue = [ASINetworkQueue queue];
[self.netWorkQueue reset];
[self.netWorkQueue setShowAccurateProgress:YES];
// 设置请求的并发数
// self.netWorkQueue maxConcurrentOperationCount =
[self createProgerssView];
[self creatLabel];
[self creaeButton];
// Do any additional setup after loading the view, typically from a nib.
}