Friday, December 17, 2010

Asynchronous download/request with Objective-C

For handling asynchronous request there is a very neat and simple mechanism in Objective-C.

Delegates

The following header file defines a protocol for the delegate and the necessary methods and data types to create a downloader that constantly pushes an update about the download state to the main thread.

Header File



Implementation



This implementation is ment for big files like 200 MB zip files. If you just want to download small stuff like an image u can instead of writing into a file use a NSData object.

Hope this tutorial did help. Feel free to post some code improvements or questions in the comments?

P.S.: Will look for a nicer way to post code in blogger. For the moment I don't have a lot of time (child n.3 was born two days ago... ;o) ) so ill leave it this way.


Cheers,

JayEs

Here the code as Pastie-link:

header-file
implementation

5 comments:

  1. very well explained. thanks for the valuable information

    ReplyDelete
  2. Hello Jayes, nice post.
    But i stuck on one problem.
    I have implement this class in my project but its delegate methods doesn't call.
    Please help me to overcome my problem

    ReplyDelete
  3. Hi,

    thanks. Did you set the delegate to self and does your class implement the DownloaderDelegate protocol?

    e.g. in your header file it should look kind of like this:

    @interface YourClass : NSObject <DownloaderDelegate>{

    and in the implementation where you use the class:

    Download* d = [[Downloader alloc] init];
    d.delegate = self;


    Cheers

    ReplyDelete
  4. UIDevice is an undeclared variable...
    i'm trying to do this on a XCode Cocoa Application - not on an Iphone or Ipad...
    any updates?

    ReplyDelete
  5. Hello Jayes, nice post.
    But i stuck on one problem.
    I have implement this class in my project but its delegate methods doesn't call.

    http://www.meetingle.com

    ReplyDelete