`
wsqwsq000
  • 浏览: 677011 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ASIHTTPRequest实现断点下载

 
阅读更多

 

 

- (IBAction)URLFetchWithProgress:(id)sender

{

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

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

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

if ([[NSFileManager defaultManagerfileExistsAtPath:tempFile]) {

[[NSFileManager defaultManagerremoveItemAtPath:tempFile error:nil];

}

[self resumeURLFetchWithProgress:self];

}

- (IBAction)stopURLFetchWithProgress:(id)sender

{

networkQueue = [[ASINetworkQueue allocinit];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(updateBandwidthUsageIndicatoruserInfo: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 allocinitWithURL:[NSURLURLWithString:@"http://9991.net/blog/mp3/2.mp3"]] autorelease];

[request setDownloadDestinationPath:[[[[NSBundle mainBundlebundlePath]

stringByDeletingLastPathComponentstringByAppendingPathComponent:@"MemexTrails_1.0b1.mp3"]];

[request setTemporaryFileDownloadPath:[[[[NSBundle mainBundlebundlePath]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:%@",[[[requesterroruserInfoobjectForKey:@"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:];

}

}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics