I am lending the code from a previous project I have done where it worked perfectly. I have tested the page separately which outputs the data using jQuery and it works fine so I know it is an issue within xcode.
I am using the following code which takes an NSDictionary containing the post data used to get the correct return data:
-(NSDictionary *)loadData:(NSDictionary *) sendDict{
SBJsonWriter *writer = [SBJsonWriter new];
NSString * jsonData = [writer stringWithObject:sendDict];
NSString * getString = [NSString stringWithFormat:@"json=%@", jsonData];
NSData *requestData = [NSData dataWithBytes: [getString UTF8String] length: [getString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://divisi.co.uk/YTA/custom_api.php"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"Returned Json: %@", returnString);
// decoding the json
NSDictionary *loginArray = [NSDictionary alloc];
loginArray = [returnString JSONValue];
if([[loginArray objectForKey:@"SC"] isEqualToString:@"200"]){
return [loginArray objectForKey:@"data"];
}
else{
return [loginArray objectForKey:@"error_message"];
}
}
When I send the post request it finds null on the php side and the json this encodes to be posted is {"method":"getWhatsOn"}
, very simple. My test php page just gets this method name and prints it back encoded correctly i.e. {"SC":"200","data":"getWhatsOn"}
. Which works in browser using jQuery as I mentioned, but in php the $_POST['method'] returns null when the request comes from the iPhone.