views:

764

answers:

3

Hi All, I am very new to the iPhone SDK and have read many threads on RESTful service integration with the iPhone. However I am none the wiser. Can you help?

I have some PHP and a MySQL database string on a server. I POST 3 pieces on information from the iPhone to the PHP which then does some thinking and chatting to the database.

It then returns 1, 2 or 3 values depending on the information POSTed and the database.

Where I am having the problem is capturing and storing the response in a way that can then be analysed on the iPhone.

I have tried returning the data as a string (echo/return), an array, and finally XML. None of which seems to work.

Does anyone have any thoughts on this, or might point me in the right direction?

The ideal situation would be to end up with an array or 3 variables that I could test with a basic if statement.

Thanks in advance.

+2  A: 

If you can send your data in JSON format, I recommend trying the Objective-C JSON framework. I've found it very easy to get started with. JSON is very easy to work with for sending data in arrays and key-value dictionaries. With the framework, you convert your json data to NSArrays or NSDictionaries with one method invocation.

There are several nice tutorials on this blog. Also, make sure to check out json.org.

Another alternative would be to have a look at the ObjectiveResource framework. It's designed to be used with Ruby on Rails based services, but if your API is truly RESTful, you should be able to use it.

Felixyz
For JSON, YAJL (http://lloyd.github.com/yajl/) also looks cool.
Felixyz
+3  A: 

Another alternative is to use the CocoaREST code available on github: http://github.com/sdegutis/CocoaREST. It was released only recently, but seems to be under active development and looks like its relatively straightforward to use.

EDIT: As suggested in another answer, yet another approach would be to use an XML format. I would suggest a specific variant of that: you can use XML, but use plist-formatted XML. That way you can circumvent the need to parse the XML yourself and simply use something like:

NSArray * myArray = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://example.com/xmlProducer"]];

(Similarly, you can create NSDictionary, NSString, etc from a URL)

Dave DeLong
+1  A: 

Send your data back as a plist (it's a subset of XML), that's easiest to parse.

Kendall Helmstetter Gelner