Which options do I have to retrieve the data from PHP to Objective-C?
Until now, I did it like:
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/~username/Proyect/LogIn.php"]];
[request setHTTPMethod:@"POST"];
NSString *post =[[NSString alloc] initWithFormat:@"Account=%@&Password=%@&submit=",[account stringValue],[password stringValue]];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *data=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
The problem with this is that the data (I mean the variable data in the code above), when I print (NSLog) it’s value, it prints everything that was printed (echo) in my PHP file, (not the return value of the PHP file, as it has to be). Is there any way to return data, for example let’s say like an object, or at least a primitive variable (boolean, strings, int. . . ), without using the echoes?
If anyone has any good and complete tutorial about how to handle data from web services, it would be helpful.
P.S.: I’m trying to program a MMORPG, I rather spend my time programming video games than playing them, that’s why I would like to retrieve the data as an object (character for example).
Output your PHP object with JSON using
echo json_encode($object). You fetch the data like you are, and then useNSJSONSerialization“decode” the JSON that PHP encoded: