views:

50

answers:

1

Hey everyone,

I'm a complete noob at objective-C, so this might be a very silly question to a lot of you.

Currently, I have a view with a 'SignIn' button, which when clicked, activates a sigupUp IBAction that I have defined below. Basically, this method needs to make a JSON call and get back some data about the user.

So, right now, my code looks something like this:

-(IBAction)signIn:(id)sender{

//run registration API call

//establish connection
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.apicallhere.com/api/auth"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];

[[NSURLConnection alloc]initWithRequest:request delegate:self];
responseData = [[NSMutableData data] retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{[responseData setLength:0];}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{[responseData appendData:data];}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{/*NSLog(@"%@",error);*/}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *response=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
....code carries on from here.
}

As you can see, the problem with my code is that even though it is initiated via the 'SignUp' method, it finishes with the 'connectionDidFinishLoading' method. I want to have all this code in a single method. Not 2 separate ones, as I want to be able to return a boolean verifying if the connection was successful or not.

If someone could please tell me how to code up this procedure into a single method, I would really appreciate it.

+1  A: 

If you really want the code all in one method, you're talking about potentially blocking all of the UI and so forth on a synchronous HTTP request method and response call.

The method to put this all "inline" is sendSynchronousRequest:returningResponse:error on NSURLConnection

[NSURLConnection sendSynchronousRequest:returningResponse:error:]

i.e.

NSError *error;
NSURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
... do something with the NSURLResponse to enjoy with your data appropriately...

I would personally encourage you to look at an alternative for most of this sort of thing towards the asynchronous methods.

heckj
In your selector name, you're missing a colon, just for clarity. Should be: `sendSynchronousRequest:returningResponse:error:` the trailing colon is important. Try and perform the selector without it, and you'll get an error :)
jer
Thanks for the catch - updated!
heckj