Passing Data From iPhone app to PHP file
What is the best way to pass data input from an iphone app to a php file?开发者_开发知识库
A NSURLRequest POST or GET call to the php file.
- (void) <functionname>:<parameters> {
NSString *urlString = [NSString stringWithFormat:@"<url with GET>",<parameters>];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; NSError * e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
If you're asking about posting the data to a web server, I'd recommend using ASIHTTPRequest to HTTP POST the data to the server in Json format. You'd then use the PHP function json_decode() to decode the data.
Json-framework is a good Json framework for the iPhone.
I like to simply make a URLRequest
, set its method to @"POST"
, and using a simple xml just send it as the body of the request. Finally with a NSURLConnection
and its delegate methods you verify and add more actions depending on the method being called at a given time.
With the connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
delegate method you can handle a response from your php side.
But if all you are doing is receiving something like an XML
, you can just make the request to the url and send it to a parser; in this case I usually use NSXMLParser
.
精彩评论