Skip to content

Instantly share code, notes, and snippets.

@ejknapp
Created December 4, 2009 19:19
Show Gist options
  • Save ejknapp/249269 to your computer and use it in GitHub Desktop.
Save ejknapp/249269 to your computer and use it in GitHub Desktop.
//POSTing a message to the server and checking the response.
//POST the message to the server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
//Extract the JSON string from the response
NSString *jsonString = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
/*
The returned JSON string will look like this:
{
"message": {
"user_first_name": "bob",
"user_last_name": "smith",
"kind": "message",
"created_at": "2009-10-27T04:11:47Z",
"body": "[email protected]",
"updated_at": "2009-10-27T04:11:47Z",
"id": 1,
"user_id": 1
}
}
- You will need to convert it to an Objective-C dictionary first.
- Then you have to extract the inner dictionary.
- You can now get the returned ID and check it.
*/
//Create a dictionary from the reponse from the server - Using TouchJSON
NSDictionary *messageDictionary = [[CJSONDeserializer deserializer]
deserializeAsDictionary:responseData
error:nil];
//Extract the inner dictionary
NSDictionary *innerDictionaray = [messageDictionary objectForKey:@"message"];
//Get the message id and inspect it
NSString *messageId = [innerDictionaray objectForKey:@"id"];
if (messageId) {
// Now you can add the new message to Core Data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment