Coder Social home page Coder Social logo

download-manager's Introduction

Download Manager

--

Introduction

This is a demonstration of the use of NSURLConnection, an iOS Cocoa class for loading a URL request, to download files from the Internet. See the URL Loading System Programming Guide for more information about the proper use and capabilities of NSURLConnection as well as that of NSURLConnectionDataDelegate.

Many simple NSURLConnection implementations load the entire file into memory while it's being downloaded. This has been designed to avoid that shortcoming, directly streaming the file to persistent storage. Furthermore, as it's streaming the contents to persistent storage, this routine also can inform the calling routine (through a delegate protocol, discussed below) of the progress of the download.

Note, since writing this class, I decided to write a NSOperation-based solution, DownloadOperation, which is far simpler, employing NSOperationQueue to take care of the management of the download operations. I would suggest you look at that solution first, before considering this particular implementation. Or, even better, consider using an actively supported networking library like AFNetworking.

Class Reference

Please see the Class Reference.

How to Use

First, you should add the Download.h, Download.m, DownloadManager.h and DownloadManager.m files to your project. As always, whenever adding files to your project, make sure the two .m files show up under "Compile Sources", in the "Build Phases" section of your target settings.

Second, declare a property for the download manager and, if you want to be informed of the progress of the downloads, conform to the DownloadManagerDelegateProtocol:

#import "DownloadManager.h"

@interface ViewController () <DownloadManagerDelegateProtocol>
@property (strong, nonatomic) DownloadManager *downloadManager;
@end

Third, start the downloads:

- (void)queueAndStartDownloads
{
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"downloads"];
        
    // an array of files to be downloaded
    
    NSArray *urlStrings = @[
        @"http://your.web.site.here.com/test/file1.pdf",
        @"http://your.web.site.here.com/test/file2.pdf",
        @"http://your.web.site.here.com/test/file3.pdf",
        @"http://your.web.site.here.com/test/file4.pdf"
    ];
    
    // create download manager instance
    
    self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];
    
    // queue the files to be downloaded
    
    for (NSString *urlString in urlStrings) {
        NSString *downloadFilename = [downloadFolder stringByAppendingPathComponent:[urlString lastPathComponent]];
        NSURL *url = [NSURL URLWithString:urlString];
        
        [self.downloadManager addDownloadWithFilename:downloadFilename URL:url];
    }
}

Fourth, if you want to be informed when the downloads complete, define a didFinishLoadingAllForManager method

- (void)didFinishLoadingAllForManager:(DownloadManager *)downloadManager
{
    // all downloads successful
}

Fifth, if you want to be informed of the success or failure of individual downloads, define

- (void)downloadManager:(DownloadManager *)downloadManager downloadDidFinishLoading:(Download *)download;
{
    // download failed
    // filename is retrieved from `download.filename`
}

- (void)downloadManager:(DownloadManager *)downloadManager downloadDidFail:(Download *)download;
{
    // download failed
    // filename is retrieved from `download.filename`
}

Sixth, and finally, if you want to be informed as the download is in progress, you can use

- (void)downloadManager:(DownloadManager *)downloadManager downloadDidReceiveData:(Download *)download;
{
    // download failed
    // filename is retrieved from `download.filename`
    // the bytes downloaded thus far is `download.progressContentLength`
    // if the server reported the size of the file, it is returned by `download.expectedContentLength`
}

By the way, regarding download.expectedContentLength, it should be noted that Apple reports that this is not entirely trustworthy:

Some protocol implementations report the content length as part of the response, but not all protocols guarantee to deliver that amount of data. Clients should be prepared to deal with more or less data.

Example

To demonstrate the use of these utility classes, this demonstration includes the following classes:

  • MasterViewController is a UITableViewController subclass that uses DownloadManager to download a series of files from the Internet and to put the resulting files in a folder in the iOS device's Documents folder;
  • DownloadCell is a UITableViewCell subclass that defines the three controls (a UIActivityIndicatorView, a UIProgressView and a UILabel) used by MasterViewController
  • AppDelegate is just the standard app delegate

--

If you have any questions, do not hesitate to contact me at:

Rob Ryan [email protected]

24 November 2012

download-manager's People

Contributors

robertmryan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.