5

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?

splattne
  • 102,760
  • 52
  • 202
  • 249
Jason
  • 17,276
  • 23
  • 73
  • 114

3 Answers3

11

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
  • 13,343
  • 3
  • 33
  • 35
2

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
  • 736
  • 1
  • 6
  • 9
1

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_Class/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_Class/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 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/

Community
  • 1
  • 1
Daniel
  • 22,363
  • 9
  • 64
  • 71