I'm another newbie to the iOS world and have been trying to figure out how NSURLConnection works with it's delegate. I'm not having much luck. After looking through several examples on the net, I created the following test class. The problem i'm having is that none of my delegate methods are ever called. My timeout loop exists an no trace messages are every displayed. I've run this through tcpmon and can see the request going out and a response being sent back, but nothing ever get's delivered to my delegate.
Can anyone tell me what I'm doing wrong.
Thanks.
Here's my code:
TestRequest.h
#import <Foundation/Foundation.h>
@interface TestRequester : NSObject <NSURLConnectionDataDelegate>
@property BOOL finished;
-(void)testRequest;
@end
And the implementation:
#import "TestRequester.h"
@implementation TestRequester
-(void)testRequest {
NSString *url = @"<your favourite URL>";
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
if (theConnection) {
NSLog(@"Connection created. Waiting....");
int count = 20;
while (!_finished && count-- > 0)
[NSThread sleepForTimeInterval:0.5];
if (_finished)
NSLog(@"Request completed");
else
NSLog(@"Request did not complete");
} else {
NSLog(@"Connection was not created!");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"-------------> Received data");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"-------------> Received response");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"-------------> connectionDidFinishLoading");
_finished = YES;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"-------------> Received error");
_finished = YES;
}
@end