views:

883

answers:

3

I have a web service developed in coldfusion that I am trying to consume on the iphone. The web service returns JSON which should be fairly simple to read. However, I have been unable to find a good simple example of an iphone app calling a web service and using the data. Are there any good tutorials or examples out there that I am just missing?

+1  A: 

There are a few ways to do this ill mention 2

1- If you are getting just some text response back you can use [NSString stringWithContentOfURL:url] this will fill the string with the response of the web request.

2- You can use NSURLRequest/NSMutableURLRequest along with NSURLConnection to make your request and get the data back, heres a ref to NSURLRequest http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest%5FClass/Reference/Reference.html, youll have to set a few properties such as the URL the request type (get, post) httpHeaders if applicable, once you have done that you can use NSURLConnection to issue the request heres a reference, http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection%5FClass/Reference/Reference.html, you can use methods such as sendSynchronousRequest or initWithRequest and start (to do an async request) which will get you your response (both cases youll get some NSData o bject back which you can translate into whatever it is its supose to be (a string or some picture data or whatever).

This question has been posted a few times in SO, just look around im sure ull find good examples, heres one link http://stackoverflow.com/questions/319463/can-i-make-post-or-get-requests-from-an-iphone-application.

Also there is a json framework out there that will parse t he JSON responses for you, heres a link talking about that http://iphone.zcentric.com/2008/08/05/install-jsonframewor/

Daniel
+9  A: 

A. Get ASIHTTPRequest.

B. Get json-framework.

C. Use A to get data from your web-service then hand it to B which returns a dictionary.

That's pretty much it.

Ramin
+2  A: 

To use JSON, you need to use a 3rd party framework, as there is no built-in support. I suggest using this http://code.google.com/p/json-framework/ as it's one of the simplest to implement. You can make the GET request just using NSURLRequest, or I'd also recommend using http://allseeing-i.com/ASIHTTPRequest/ if you need to make more complex requests, for example using basic authentication.

I wrote a blog post that has step-by-step instructions on using JSON from Cocoa/Objective-C with an example:

http://zachwaugh.com/2009/01/how-to-use-json-in-cocoaobjective-c/

Zach Waugh