1 - (IBAction)URLFetchWithProgress:(id)sender
2 {
3 [startButton setTitle:@"Stop" forState:UIControlStateNormal];
4 [startButton addTarget:self action:@selector(stopURLFetchWithProgress:)
forControlEvents:UIControlEventTouchUpInside];
5 NSString *tempFile = [[[[NSBundle mainBundle]
bundlePath]stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.download"];
6 if ([[NSFileManager defaultManager] fileExistsAtPath:tempFile])
{
7 [[NSFileManager defaultManager] removeItemAtPath:tempFile error:nil];
8 }
9
10 [self resumeURLFetchWithProgress:self];
11 }
12
13 - (IBAction)stopURLFetchWithProgress:(id)sender
14 {
15 networkQueue = [[ASINetworkQueue alloc] init];
16 timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(updateBandwidthUsageIndicator)
userInfo:nil repeats:YES];
17 timer = nil;
18 [startButton setTitle:@"Stop" forState:UIControlStateNormal];
19 [startButton addTarget:self action:@selector(URLFetchWithProgress:)
forControlEvents:UIControlEventTouchUpInside];
20 [networkQueue cancelAllOperations];
21 [resumeButton setEnabled:YES];
22 }
23
24 - (IBAction)resumeURLFetchWithProgress:(id)sender
25 {
26 [resumeButton setEnabled:NO];
27 [startButton setTitle:@"Start" forState:UIControlStateNormal];
28 [startButton addTarget:self action:@selector(stopURLFetchWithProgress:)
forControlEvents:UIControlEventTouchUpInside];
29 [networkQueue cancelAllOperations];
30 [networkQueue setShowAccurateProgress:YES];
31 [networkQueue setDownloadProgressDelegate:progressIndicator];
32 [networkQueue setDelegate:self];
33 [networkQueue setRequestDidFinishSelector:
@selector(URLFetchWithProgressComplete:)];
34
35 ASIHTTPRequest *request = [[[ASIHTTPRequest alloc]
initWithURL:[NSURLURLWithString:
@"http://9991.net/blog/mp3/2.mp3"]] autorelease];
36 [request setDownloadDestinationPath:[[[[NSBundle mainBundle]
bundlePath]stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"MemexTrails_1.0b1.mp3"]];
37 [request setTemporaryFileDownloadPath:[[[[NSBundle mainBundle]
bundlePath]stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"MemexTrails_1.0b1.zip.down"]];
38 [request setAllowResumeForFileDownloads:YES];
39 [networkQueue addOperation:request];
40 [networkQueue go];
41 }
42
43 - (void)URLFetchWithProgressComplete:(ASIHTTPRequest *)request
44 {
45 if ([request error]) {
46 fileLocation.text=[NSString stringWithFormat:@"An error occurred: %@",
[[[requesterror] userInfo] objectForKey:@"Title"]];
47 }
else {
48 fileLocation.text=[NSString stringWithFormat:
@"File downloaded to %@",[requestdownloadDestinationPath]];
49 }
50 [startButton setTitle:@"Start" forState:UIControlStateNormal];
51 [startButton addTarget:self action:@selector(URLFetchWithProgress:)
forControlEvents:UIControlEventTouchUpInside];
52 }
53
54 - (IBAction)throttleBandwidth:(id)sender
55 {
56 if ([(UIButton *)sender state] ==YES)
{
57 [ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount];
58 }
else {
59 [ASIHTTPRequest setMaxBandwidthPerSecond:0];
60 }
61 }