0

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
DaveR
  • 1,295
  • 1
  • 13
  • 35

2 Answers2

0

you need to set the connections delegate as being of type NSURLConnectionDelegate not NSURLConnectionDataDelegate in your header. As far as i can see.

@interface TestRequester : NSObject <NSURLConnectionDelegate>

Refer to the part under connection protocols on this page :) https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLConnection

Bergasms
  • 2,203
  • 1
  • 18
  • 20
  • 2
    I get the same results whether I use NSURLConnectionDelegate or NSURLConnectionDataDelegate. – DaveR Nov 13 '12 at 20:37
0

Looks like your connection falls out of scope. You're probably using ARC.

TestRequest.h

#import <Foundation/Foundation.h>

@interface TestRequester : NSObject <NSURLConnectionDelegate>
{
    NSURLConnection *theConnection;
}
@property BOOL finished;

-(void)testRequest;
@end

-(void)testRequest {

NSString *url = @"<your favourite URL>";
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                            timeoutInterval:60.0];
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!");
}

}
estobbart
  • 1,137
  • 12
  • 28
  • 1
    How does the connection fall out of scope? The connection is created in the testRequest method, which doesn't exit until the "while" loop finishes. – DaveR Nov 13 '12 at 00:33
  • In any event, no change in results doing it your way. – DaveR Nov 13 '12 at 20:37
  • I missed that loop the first time I looked at it. Get rid of that loop, it's blocking the thread. I've edited the code. – estobbart Nov 14 '12 at 21:12
  • Using Arc prevents this from happening? What a bad solution! Arc is something that everyone should and is using. . . – coolcool1994 May 07 '13 at 14:23