donga donga - 3 months ago 18
iOS Question

How can I print the response from my simple, working POST method

I have a post method that looks like this:

NSString *totalPostURL = [NSString stringWithFormat:@"%@registerDevice",self.textUrl];

NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:totalPostURL]];


NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:self.finalDict options:0 error:&error];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


The code works fine but I have no idea how to print the response from this post. Any suggestions welcome as I'm new to iOS development.

Answer

You can print the response as below:

NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

NSLog(@"Response:%@",returnString);

But Remember as you are a newbie,don't forget to read the tutorial for calling webservices.Interacting with webservices.

Comments