Hello experts!
I'm trying to create a very simple app which connects to an URL and gets the contents of that URL, in this case a simple xml document. My problem is that that the request never seems to gets sent.
I have created a new project(Foundation tool) which runs from a main file. I have set the delegate of the NSURLConnection to self and implemented the necessary delegate methods. The problem is that these methods never get called. It doesn't matter if I change the URL to some bogus one(as in the example below). It seems to me that the application finish launching without sending or waiting for the response. Could it be that my application creates a thread for the request and then finishes without waiting for the response?? I know that the class is running since i get the NSLOG statements like "Created connection " but no NSLOG statements from within the delegate methods.
What am I missing?
#import "NetworkController.h"
@implementation NetworkController
- (id)init
{
if(self = [super init])
{
}
return self;
}
- (void)dealloc
{
NSLog(@"Calling dealloc");
[super dealloc];
}
- (void) performConnection
{
NSMutableData *receivedData;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
@"http://www.test.php?some variables"]];
NSMutableURLRequest *connectionRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[url release];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:connectionRequest delegate:self startImmediately:YES];
if (!theConnection)
{
NSLog(@"Failed to submit request");
}
if(theConnection)
{
receivedData=[[NSMutableData data] retain];
NSLog(@"Created connection.");
NSLog(@"--------- Request submitted ---------");
NSLog(@"receivedData: %@", receivedData);
}
[connectionRequest release];
}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Received response: %@", response);
NSLog(@"Received response, connection retain count is %d",[connection retainCount]);
}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Connection received data, retain count: %d", [connection retainCount]);
}
#pragma mark NetworkController Delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"finished connection retain count: %d", [connection retainCount]);}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error receiving response: %@", error);
[connection release];
}
@end
#import <Foundation/Foundation.h>
#import "NetworkController.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NetworkController *n = [[NetworkController alloc] init];
n.performConnection;
[n release];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}