ASIHTTPRequest实现断点下载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASIHTTPRequest实现断点下载相关的知识,希望对你有一定的参考价值。

- (IBAction)URLFetchWithProgress:(id)sender

{

[startButton setTitle:@"Stop" forState:UIControlStateNormal];

[startButton addTarget:self action:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];

NSString*tempFile = [[[[NSBundle mainBundle] bundlePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"];

if ([[NSFileManager defaultManager] fileExistsAtPath:tempFile]) {

[[NSFileManager defaultManager] removeItemAtPath:tempFile error:nil];

}

[self resumeURLFetchWithProgress:self];

}

- (IBAction)stopURLFetchWithProgress:(id)sender

{

networkQueue = [[ASINetworkQueue alloc] init];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(updateBandwidthUsageIndicator) userInfo:nil repeats:YES];

timer = nil;

[startButton setTitle:@"Stop" forState:UIControlStateNormal];

[startButton addTarget:self action:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];

[networkQueue cancelAllOperations];

[resumeButton setEnabled:YES];

}

- (IBAction)resumeURLFetchWithProgress:(id)sender 

{

[resumeButton setEnabled:NO];

[startButton setTitle:@"Start" forState:UIControlStateNormal];

 [startButton addTarget:self action:@selector(stopURLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];

[networkQueue cancelAllOperations];

[networkQueue setShowAccurateProgress:YES];

[networkQueue setDownloadProgressDelegate:progressIndicator];

[networkQueue setDelegate:self];

[networkQueue setRequestDidFinishSelector:@selector(URLFetchWithProgressComplete:)];

ASIHTTPRequest*request=[[[ASIHTTPRequest alloc] initWithURL:[NSURLURLWithString:@"http://9991.net/blog/mp3/2.mp3"]] autorelease];

[request setDownloadDestinationPath:[[[[NSBundle mainBundle] bundlePath]

stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"MemexTrails_1.0b1.mp3"]];

[request setTemporaryFileDownloadPath:[[[[NSBundle mainBundle] bundlePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.down"]];

[request setAllowResumeForFileDownloads:YES];

[networkQueue addOperation:request];

[networkQueue go];

}

- (void)URLFetchWithProgressComplete:(ASIHTTPRequest *)request

{

if ([request error]) {

fileLocation.text=[NSString stringWithFormat:@"An error occurred:%@",[[[requesterror] userInfo] objectForKey:@"Title"]];

} else {

fileLocation.text=[NSString stringWithFormat:@"File downloaded to %@",[requestdownloadDestinationPath]];

}

[startButton setTitle:@"Start" forState:UIControlStateNormal];

[startButton addTarget:self action:@selector(URLFetchWithProgress:)forControlEvents:UIControlEventTouchUpInside];

}

- (IBAction)throttleBandwidth:(id)sender

{

if ([(UIButton *)sender state] ==YES) {

[ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount];

} else {

[ASIHTTPRequest setMaxBandwidthPerSecond:];

}

}

以上是关于ASIHTTPRequest实现断点下载的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 3.0 断点续传 使用记录

ASIHTTPRequest框架的简单使用

NSURLSession 断点下载

使用ASIHTTPRequest下载音频

Android 多线程下载,断点续传,线程池

大文件上传下载分片断点续传教程