1

I have a ViewController declared as:

@interface DownloadViewController : UIViewController 
           <UITableViewDataSource, UITableViewDelegate>

and I want to use NSURLConnection to download files. NSURLConnection simply "doesn't start", the delegate methods don't work (for example connection:didReceiveResponse is never called) . I noticed in some sample code that the class was subclassing NSObject instead of UIViewController.

How do I combine it? I want to use ViewController methods but then I can't use NSURLConnection.

It's not so easy to find a fully explained example how to download file with NSURLConnection. Everyone only concentrates on the easy methods like didReceiveResponse.

yosh
  • 3,245
  • 7
  • 55
  • 84

3 Answers3

3

If you're having problems, you could consider using the well regarded ASIHTTPRequest library to manage your download. It takes care of everything for you.

For example, just 2 lines will do it.

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:fullPathOfWhereToStoreFile];
Roger
  • 15,793
  • 4
  • 51
  • 73
  • +1. I haven't touched NSURLConnection in months and months and months, since I discovered ASIHTTPRequest. – Dan Ray Apr 27 '11 at 12:09
  • If I'm downloading 50 files with 1 click I'm just calling 50 requests? I don't remember now but there was something that I was worried about when checking this ASI solution. – yosh Apr 27 '11 at 12:16
  • I download hundreds on a single click (only on WIFI mind you) but I do it serially - ie I put the requests into my own queue and as each one finishes, I kick off the next in the queue. ASIHTTPrequest can actually do all this for you I think as it has queuing too. – Roger Apr 28 '11 at 09:52
3

Using a UIViewController instead of an NSObject should not be your problem here ! I'm using a NSURLConnection in an UIViewController with no issue ! Here is a part of my code (not sure it will compile as it is) :

//
//  MyViewController.h
//

#import <Foundation/Foundation.h>

@interface MyViewController : UIViewController {
    @protected
    NSMutableURLRequest* req;
    NSMutableData* _responseData;
    NSURLConnection* nzbConnection;
}

- (void)loadFileAtURL:(NSURL *)url;

@end

-

//
//  MyViewController.m
//

#import "MyViewController.h"

@implementation MyViewController

- (void)loadView {  
// create your view here
}

- (void) dealloc {
    [_responseData release];

    [super dealloc];
}

#pragma mark -

- (void)loadFileAtURL:(NSURL *)url {
    // allocate data buffer
    _responseData = [[NSMutableData alloc] init];

    // create URLRequest
    req = [[NSMutableURLRequest alloc] init];
    [req setURL:_urlToHandle];

    nzbConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
    [req release];
    req = nil;
}


#pragma mark -

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Append data in the reception buffer
    if (connection == nzbConnection)
        [_responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (connection == nzbConnection) {
        [nzbConnection release];
        nzbConnection = nil;

        // Print received data
        NSLog(@"%@",_responseData);

        [_responseData release];
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // Something went wrong ...
    if (connection == nzbConnection) {
        [nzbConnection release];
        [_responseData release];
    }
}

@end

If you plan to download large files, consider storing the received packets in a file instead of storing it in memory !

Sylverb
  • 939
  • 6
  • 14
  • This code is to download a file? I don't see where you can tell it what directory to download the file to. – Marcel Marino Oct 03 '12 at 19:28
  • 1
    @MarcelMarino : file is stored in the NSMutableData *_responseData object. As said at the end of my comment, if you want to store result in a file, you should store the result in a file (which is not done here but it's not hard to find how to achieve this ;) – Sylverb Dec 24 '12 at 17:21
1

Use "NSURLConnection asynchronously" search for the term and you'll find source. Or just NSURLConnection.

For example:

NSURLConnection NSURLRequest proxy for asynchronous web service calls

Using NSURLConnection from apple with example code

Objective-C Programming Tutorial – Creating A Twitter Client Part 1

Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • It doesn't really answer my question ;) First 2 links don't mention ViewController at all, but maybe I can learn something from this Twitter Client tutorial.. – yosh Apr 27 '11 at 12:12
  • @yosh: Read the second link (Apple's link), they have explained it in very simple manner,It's not ViewController,you could use it with UIView as well, all you need to have delegate methods in place to update you about the progress of your NSURLConnection request, for that the third link will helpful for you, Thanks – Jhaliya - Praveen Sharma Apr 27 '11 at 12:20
  • I've read the Apple link before and I'm still confused how to start the download with simply some button press. That is what I need to know, I'll figure out the rest about saving files and multiple file download. Checking Twitter soludion, thanks! – yosh Apr 27 '11 at 12:38