#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
@interface ViewController : UIViewController<ASIHTTPRequestDelegate>
@property (nonatomic, retain) UIProgressView *progressView;
@property (nonatomic, retain) NSString *downLoadpath;
@property (nonatomic, retain) ASIHTTPRequest *request;
@end
#import "ViewController.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)dealloc
{
[_progressView release];
[_downLoadpath release];
[_request clearDelegatesAndCancel];
[_request release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(5, 200, 320, 10)];
_progressView.progress = 0.0f;
[self.view addSubview:_progressView];
UIButton *startDownLoad = [UIButton buttonWithType: UIButtonTypeRoundedRect];
startDownLoad.frame = CGRectMake(100, 250, 60, 40);
[startDownLoad addTarget:self action:@selector(start:) forControlEvents: UIControlEventTouchUpInside];
[startDownLoad setTitle:@"开始" forState:UIControlStateNormal];
[self.view addSubview:startDownLoad];
UIButton *pauseDownLoad = [UIButton buttonWithType: UIButtonTypeRoundedRect];
pauseDownLoad.frame = CGRectMake(220, 250, 60, 40);
[pauseDownLoad addTarget:self action:@selector(pause:) forControlEvents:
UIControlEventTouchUpInside];
[pauseDownLoad setTitle:@"暂停" forState:UIControlStateNormal];
[self.view addSubview:pauseDownLoad];
}
- (void)start:(UIButton*)sender
{
[self loadData];
}
- (void)pause:(UIButton*)sender
{
[_request clearDelegatesAndCancel];
}
- (void)loadData
{
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@", path);
self.downLoadpath = [path stringByAppendingPathComponent:@"music"];
NSString *tempPath = [path stringByAppendingPathComponent:@"musicTmp"];
NSURL *url = [NSURL URLWithString:@"http://hugemaxiao.co/COFFdD0xMzcxOTU2NDg3Jmk9MTIwLjE5Ny4xMzEuNDEmdT1Tb25ncy92MS9mYWludFFDLzhjLzc0N2E4MjVlMzQ4YjljNWEzZTk4NjE3OGZkNGU2ZjhjLm1wMyZtPTY1NTc3MTU0NjY2ODliYmI4MzBiZTFjZDI3ZTQwOWQ0JnY9ZG93biZuPbfwy7Umcz26zurJw/omcD1z.mp3"];
_request = [[ASIHTTPRequest alloc]initWithURL:url];
_request.delegate = self;
//设置下载路径
[_request setDownloadDestinationPath:_downLoadpath];
//设置缓存路径
[_request setTemporaryFileDownloadPath:tempPath];
//下载进度代理可以直接用UIProgressView对象,它会自动更新,如果你想做更多的处理
//就必须用我们自定义的类,只要我们的类里实现了setPorgress:方法
_request.downloadProgressDelegate = _progressView;
//设置支持断点续传
[_request setAllowResumeForFileDownloads:YES];
//[_queue addOperation:request];
[_request startAsynchronous];
//[request release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setPorgress:(CGFloat)progress
{
_progressView.progress = progress;
}
- (void)requestStarted:(ASIHTTPRequest *)request
{
}
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
}
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL
{
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[request release];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
}
- (void)requestRedirected:(ASIHTTPRequest *)request
{
}
@end