ASIHTTPRequest 1.x:断点续传

 

撰写于 2012 年 11 月 6 日 | 转载自 :  http://itjoy.org/?p=469

ASIHTTPRequest支持断点续传,断点续传的时候我们要设置一个临时容器来接收数据,当数据完全下载完毕的时候,文件将从临时文件夹移入到指定文件夹,自动删除临时文件夹中的内容。

代码实例:下载一首歌曲

////////////////
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //progress
    self.progressIndicator = [[[UIProgressView alloc]initWithFrame:CGRectMake(10, 80, 300, 10)]autorelease];
    [self.view addSubview:self.progressIndicator];
    
    ///startBtn
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    startBtn.frame = CGRectMake(20, 100, 100, 60);
    [startBtn setTitle:@"Start" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(downloadStart:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startBtn];
    
    
    UIButton *pauseBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pauseBtn.frame = CGRectMake(200, 100, 100, 60);
    [pauseBtn setTitle:@"Pause" forState:UIControlStateNormal];
    [pauseBtn addTarget:self action:@selector(downloadPause:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pauseBtn];
    
}

- (void)downloadStart:(UIButton *)btn
{
    ///创建请求
    self.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://zhangmenshiting.baidu.com/data2/music/35417538/13880429190800192.mp3?xcode=ead0f4d3b15d47b6273650a58d1b4b7f"]];
    ///设置代理
    self.request.delegate = self;
    //进度显示
    self.request.downloadProgressDelegate = self.progressIndicator;
    ///另一种获取进度的方法,需要遵守ASIProgressDelegate协议
    //self.request.downloadProgressDelegate = self;
    ///精确显示进度信息
    self.request.showAccurateProgress = YES;
    //文件存储目录
    NSString *savePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"safe&sound.mp3"];
    NSLog(@"savePath=%@",savePath);
    ///文件临时目录
    NSString *temp = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"temp"];
    //
    NSString *tempPath = [temp stringByAppendingPathComponent:@"temp.mp3"];
    NSLog(@"tempPaht=%@",tempPath);
    
    //如果临时目录不存在则创建
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL fileExists = [fileManager fileExistsAtPath:tempPath];
    if (!fileExists) {
        [fileManager createDirectoryAtPath:temp withIntermediateDirectories:YES attributes:nil error:nil];
    }
    //设置存储目录
    self.request.downloadDestinationPath = savePath;
    //设置临时目录
    self.request.temporaryFileDownloadPath = tempPath;
    //允许断点续传
    self.request.allowResumeForFileDownloads = YES;
    //下载完毕执行
    self.request.didFinishSelector = @selector(FetchDataFinished:);
    //下载失败执行
    self.request.didFailSelector = @selector(FetchDataFailed:);
    //下载信息上下文
    self.request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"safe&sound",@"music", nil];
    //开始异步下载
    [self.request startAsynchronous];
    
    
}

#pragma mark -- pause method
- (void)downloadPause:(UIButton *)btn
{
    [self.request clearDelegatesAndCancel];
}

#pragma mark -- request delegate
- (void)requestStarted:(ASIHTTPRequest *)request
{
    NSLog(@"request start");
}
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
    NSLog(@"receive responseHeader");
}

//- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
//{
    //如果自己实现那个进度条,就遵守ASIProgressDelegate 实现此方法即可,获取大小,更新进度条
//    NSLog(@"total=%lld",request.totalBytesRead);
//    NSLog(@"length=%lld",request.contentLength);
//}

#pragma mark -- finish & fail
- (void)FetchDataFinished:(ASIHTTPRequest *)request
{
    NSLog(@"finished");
    NSLog(@"reqest.userinfo=%@",[request.userInfo objectForKey:@"music"]);
}

- (void)FetchDataFailed:(ASIHTTPRequest *)request
{
    NSLog(@"failed");
    NSLog(@"error=%@",[request.error localizedDescription]);
}
原文地址:https://www.cnblogs.com/allanliu/p/4222194.html