3

I have the following 2 methods:

-(void)authenticateUserToGoogle:(NSString *)userName withPassword:(NSString *)password {


    NSString *URLstr = GOOGLE_CLIENT_LOGIN;
    URLstr = @"http://www.google.com/ig/api?stock=AAPL";
    NSURL *theURL = [NSURL URLWithString:URLstr];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (!theConnection) {
        NSLog(@"COuldn't register device information with Parking Server");
    } else {
        NSLog(@"Got a connection!!");
        NSMutableData       *_responseData = [NSMutableData data];
        NSLog(@"respone_data = %@",_responseData);

    }
    }

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
        NSLog(@"GOT A 'FUCKED' STATUS CODE");

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else if (200 == statusCode) {
        NSLog(@"GOT A 'OK' RESPONSE CODE");

    }

}

If I call the authenticateUserToGoogle method as an instance method like this:

[self authenticateUserToGoogle:user withPassword:password]

I get the the following output:

2011-08-12 00:14:08.490 stcoks[81272:f203] Got a connection!!
2011-08-12 00:14:08.492 stcoks[81272:f203] respone_data = <>
2011-08-12 00:14:08.726 stcoks[81272:f203] GOT A 'OK' RESPONSE CODE

However, if I change the authenticateUserToGoogle method to be a class method by simply changed the "-" to "+" in the method signature and then call it like this:

[MasterViewController authenticateUserToGoogle:user withPassword:password]

I get the following output:

2011-08-12 00:14:08.490 stcoks[81272:f203] Got a connection!!
2011-08-12 00:14:08.492 stcoks[81272:f203] respone_data = <>

In other words it seems like the with the class method, the delegate method connection didReceiveResponse never gets called!!

Can anyone explain this behavior to me? Thanks!

ennuikiller
  • 46,381
  • 14
  • 112
  • 137

4 Answers4

5

When you start the NSURLConnection with delegate:self, that sets up the delegate object which will receive the connection:didReceiveResponse: message. If you use self within a class method, this method will also be called as a class method.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
2

You're setting the delegate to self in your method. If it's a class method, self doesn't hold an instance of the class. I'd guess it's either the class itself, or nil.

Try changing both to class methods.

Nathanial Woolls
  • 5,231
  • 24
  • 32
2

If you change the authenticateUserToGoogle method to be a class method by simply changed the "-" to "+" in the method signature, then change the connection:didReceiveResponse: delegate method's "-" to "+" too. So your code looks like,

+ (void)authenticateUserToGoogle:(NSString *)userName withPassword:(NSString *)password {

    // Your code here
}

+ (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    // Your code here
}
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
1

Unless you're also changing the delegate method to be a class method, it wouldn't be called since the delegate (the class) does not respond to the message — only its instances do.

Chuck
  • 234,037
  • 30
  • 302
  • 389