Life & Technology

Handful lessons in different areas of technology and life in general.

Tag Archives: UIProgressView

GCNetworkKit: Download with progress via UIAlertView

For the past few months, I’ve been using this simple to use yet powerful HTTP set of classes called GCNetworkKit to communicate to your server with ease. For a brief description from the github page:

GCNetworkKit is a helpful set of classes for interacting with a http server. All classes make use of ARC. In order to use it you need to drag the folder into your xcode project. You also need to add the MobileCoreServices and the SystemConfiguration framework to your project.

And with this, I’ve come up with a simple example that demonstrate the ease of use of this library.

UIAlertView + UIProgressView

For this demo, we want to have a file download functionality that we need to display via UIAlertView with additional UIProgressView to view the progress and a UILabel to determine the total percentage completion. These two subviews will be attached to our UIAlertView and will be used during the file downloading. If you downloaded the app called CityMaps2Go, the way they present their maps download is exactly what we’ll be creating.

Our initial screen will look like this:

Next, we will hook our UIButton to trigger the display of UIAlertView on the screen and immediately start the process.

  1. -(IBAction)download:(id)sender
  2. {
  3.     NSURL *fileURL = [NSURL URLWithString:@”http://YOUR_URL_HERE/PATH/TO/FILE.pdf”%5D;
  4.     downloadAlertView = [[UIAlertView alloc] initWithTitle:@”Downloading…”
  5.                                                     message:@”             “
  6.                                                    delegate:self
  7.                                           cancelButtonTitle:@”Cancel”
  8.                                           otherButtonTitles:nil];
  9.     [progressView setProgress:0];
  10.     [downloadAlertView addSubview:progressView];
  11.     [downloadAlertView addSubview:progressLabel];
  12.     [progressView setProgressViewStyle: UIProgressViewStyleBar];
  13.     [downloadAlertView show];
  14.     [self downloadDataAtURL:fileURL];
  15. }

After adding the UIProgressView and UILabel on our UIAlertView, we will call our method that will actually perform the file downloading. Line 16 will start to call GCNetworkKit’s block-based methods as follows:

  1. – (void)downloadDataAtURL:(NSURL *)url {
  2.     __GC_weak ViewController *weakReference = self;
  3.     weakReference.progressLabel.text = @”Preparing …”;
  4.     [self performSelectorOnMainThread:@selector(updateStatusLabel:) withObject:@”> Status: Download Starting” waitUntilDone:NO];
  5.     GCNetworkDownloadRequest *request = [GCNetworkDownloadRequest requestWithURL:url];
  6.     request.autoDeleteTMPFile = YES;
  7.     request.loadWhileScrolling = YES;
  8.     request.continueInBackground = YES;
  9.     request.progressHandler = ^(CGFloat progress){
  10.         NSLog(@”Progress on downloading data: %f.”, progress);
  11.         [weakReference updateProgress:progress];
  12.     };
  13.     request.downloadCompletionHandler = ^(NSString *filePath){
  14.         self.requestHash = nil;
  15.         [self performSelectorOnMainThread:@selector(updateStatusLabel:) withObject:@”> Status: Download Completed.” waitUntilDone:NO];
  16.         [downloadAlertView dismissWithClickedButtonIndex:0 animated:YES];
  17.         NSLog(@”Downlaoded data and saved at: %@.”, filePath);
  18.         UIAlertView *success = [[UIAlertView alloc] initWithTitle:@”Download Status” message:@”File successfully downloaded!” delegate:nil cancelButtonTitle:@”Great!” otherButtonTitles: nil];
  19.         [success show];
  20.     };
  21.     self.requestHash = [[GCNetworkRequestQueue sharedQueue] addRequest:request];
  22. }

You see on Line 11, we don’t have to worry getting the current progress status of the file being downloaded, GCNetworkKit already has this functionality built-in. And on Line 15 is the completionHandler callback that we can use to process the file being downloaded or even display another UIAlertView that will popup the completion alert message.

And if we like to cancel the download process, we only need to call cancelRequestWithHash: method to immediately halt the current operation. This is possible because every request that is put in the queue automatically has a unique id in the form of hash.

  1. – (void) cancelDownload
  2. {
  3.     [[GCNetworkRequestQueue sharedQueue] cancelRequestWithHash:self.requestHash];
  4.     self.requestHash = nil;
  5. }

Using GCNetworkKit library, we achieved our task with minimal effort in coding and the result is exactly what we expected!

Screenshots for our demo project

The XCode project is compiled under ARC and available for download here.

Advertisements