anyone have experience with iOS programming

464 Views | 1 Replies | Last: 9 yr ago by khaos288
khaos288
How long do you want to ignore this user?
AG
I'm new to it for work, and I'm wondering what function to use for sending a JSON object to a URL.

I've googled and seen 1000 examples of a post method, but they're all different. Anyone have a good example?
rynning
How long do you want to ignore this user?
AG
I know a little. I use something like the following:
NSString *params = [NSString stringWithFormat:@"data1=%@&data2=%@&data3=%@", a, b, c];

// Create the URL request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mysite.com/requestpage"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Then I have the following delegate (callback) methods ready to go: didReceiveResponse, didReceiveData, willCacheResponse, didFailWithError, and (the big one) connectionDidFinishLoading.

As for handling JSON response containing objects (or an array), I think you used to have to use a 3rd party library, but now all you have to do is convert the response to an NSDictionary or NSMutableDictionary (or NSArray or NSMutableArray) using something like the following:

NSError *jsonParsingError = nil;
myDictionary = [NSJSONSerialization JSONObjectWithData:jsonResponse options:NSJSONReadingMutableContainers error:&jsonParsingError];

Then it's very easy to get objects and arrays out of it:

myDictionary[@"result"]
myDictionary[@"list"][3]
khaos288
How long do you want to ignore this user?
AG
Thanks for the reply!

I actually ended up using AFNetworking, which I highly recommend.
Refresh
Page 1 of 1
 
×
subscribe Verify your student status
See Subscription Benefits
Trial only available to users who have never subscribed or participated in a previous trial.