views:

1181

answers:

3

Hi, I want my application to download some data from the internet, in iPhone SDK documentation i found NSURLConnection class, which is used for downloading, Am i right? I wrote the same code as it is in the documentation and ran it. Connection was created successfully, but no data were downloaded. connectionDidFinishLoading is fired after sec or two but with no data in result. Problem is, that didRecieveData method is never fired. I dont know why, i searched the internet, but every result was the same code as it is in the documentation. Could you give an advice please? Thanks for every reply My downloader class source code is below.

Downloader.h

@interface Downloader : NSObject {
    NSURLConnection *conn;

    //Array to hold recieved data
    NSMutableData *recievedData;
}

@property (nonatomic, retain) NSURLConnection *conn;
@property (nonatomic, retain) NSMutableData *recievedData;

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

@end

Downloader.m

#import "Downloader.h"
@implementation Downloader
@synthesize recievedData, conn;

- (void)connection:(NSURLConnection *)connection didRecieveResponse:(NSURLResponse *)response
{
    NSLog(@"did recieve response");

    [recievedData release];
    recievedData = nil;
}

- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)data
{
    NSLog(@"did recieve data");
    //Append the new data to the recieved data
    [recievedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //Release the connection and the data object
    [connection release];
    [recievedData release];

    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription],
       [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //ToDo with data
    //[recievedData writeToFile:@"data" atomically:YES];
    NSLog(@"downloaded");
    NSLog(@"%u", [recievedData length]);
    //Release the connection and the data object
    [connection release];
    [recievedData release];
}

- (void)downloadContentsOfUrl:(NSURL *)url
{
    //Create the connection
    //Create the request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url 
            cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

        //Create the connection with the request and start loading the data
    conn =  [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self 
                startImmediately:YES];
    if(conn)
    {
     //Create the NSMutableData that will hold the recieve data
     recievedData = [[NSMutableData data] retain];
     NSLog(@"Connection success!");
    }
    else
    {
     NSLog(@"Can't download this file!");
    }   
}

- (void)dealloc
{
    [conn release];
    [recievedData release];

    [super dealloc];
}
A: 

You've misspelt "receive":

// Your signature
- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)data;

// Correct signature
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
Nathan de Vries
A: 

You have a typo in the name of your didReceiveData method (i before e, except after c :-)

Thus it will look like your class doesn't implement that (optional) selector and it will be silently ignored.

frankodwyer
A: 

Do you know how to download an entire folder at once as opposed to each file - one by one?

I need to be able to do this but I don't know if it is even possible.