I am trying to send JSON from my iOS. This is what I have so far:
+ (NSDictionary *)SendJSON:(NSString *)jsonString toAddress:(NSString *)address
{
NSData *postData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:address]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:POSTReply options:kNilOptions error:nil] );
NSDictionary *jsonresponse = POSTReply ? [NSJSONSerialization JSONObjectWithData:POSTReply options:kNilOptions error:nil] : nil;
return jsonresponse;
}
It works fine if I send the jsonString like this:
NSString *jsonString = [NSString stringWithFormat:@"username=%@&password=%@", self.txtUsername.text, self.txtPassword.text];
But If I try to send using the JSON format:
@"{ \"username\" : \"%@\", \"password\" : \"%@\" }"
it does not work any ideas why?
PS: This information is being sent to a Rails server.
stringWithFormat
. The two would not be interchangeable -- they'd have to be transmitted differently.