I rewrite one class with async connection, and add progress block and pause/resume.

Example usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NSString *comboUrl = @"http://support.apple.com/downloads/DL1484/en_US/MacOSXUpdCombo10.7.3.dmg";
AsyncURLConnection *aConnection = [AsyncURLConnection request:comboUrl
                  completeBlock:^(NSData *data, NSString *url) {
                      [data writeToFile:filename atomically:NO];
                  } errorBlock:^(NSError *error) {
                      NSLog(@"Error %f", error);
                  } progress:^(float progress) {
                      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                          /* process downloaded data in Concurrent Queue */
                          dispatch_async(dispatch_get_main_queue(), ^{
                              /* update UI on Main Thread */
                          });
                      });
                      NSLog(@"progress %f", progress);
                  }];

Very easy to use blocks of AsyncURLConnection (aka async URLConnection) and NSProgressIndicator indicator. You can implement progress bar of downloading in your mac os x application like this example.

1
2
3
4
5
6
7
8
NSProgressIndicator *horizontal = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(200, 300, 200, 12)];
    [horizontal setStyle:NSProgressIndicatorBarStyle];
    [horizontal setIndeterminate:NO];
    [horizontal setControlSize:NSSmallControlSize];
    [horizontal setDisplayedWhenStopped:YES];
    [horizontal setMaxValue:1];
    [self addSubview:horizontal];
    [horizontal release];

And just set progress value.

1
2
3
4
5
6
7
8
9
         progress:^(float progress) {
                          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                          /* process downloaded data in Concurrent Queue */
                          dispatch_async(dispatch_get_main_queue(), ^{
                              /* update UI on Main Thread */
                               [horizontal setDoubleValue:progress];
                          });
                      });
                  }];


Result:

You can modify request header in block like in this example:

1
2
3
4
5
6
7
   AsyncURLConnection *aURLConnection = [AsyncURLConnection request:url_string
                 modifyRequest:^(NSMutableURLRequest *request) {
                      [request setValue:@"iTunes/10.5.3 (Macintosh; Intel Mac OS X 10.7.3) AppleWebKit/534.53.11"
                     forHTTPHeaderField:@"User-Agent"];
                  }
                  completeBlock:^(NSData *data, NSString *url) { }
                     errorBlock:^(NSError *error) { }];

pause and resume

1
2
3
4
5
[aURLConnection pause];

if([aURLConnection isPause]) {
    [aURLConnection resume];
}