iOS 断点续传

#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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值