views:

67

answers:

2

I am in the process of building my first iPhone app (xCode,objective-c) which has to communicate with an existing website using either a web service or WCF service. I have looked primarily at using a WCF service hosted in the website which can send and receive JSON (making it lightweight as opposed to the overhead of SOAP). However I am finding it increasingly difficult to get things working on the iPhone side without having to use various 3rd party wrappers such as the JSON Framework and ASIHTTPRequest.

Can somebody tell me if I am going in the right direction and using the right tools as this seems awfully complicated for something which I thought would have been relatively straight forward. Maybe it’s because I am used to .NET C# where lots of this stuff is done for you out of the box.

ie If I were to send back to my service the following piece of JSON taken from the following example JSON example

{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}

A: 

I would use straight HTTP protocol. No JSON or SOAP.

There are a lot of classes to handle HTTP like NSURLConnection.

If you are stuck with JSON you can use some C/C++ JSON library.

If you are stuck with SOAP you can use Apache's AXIS or some other C/C++ SOAP client library.

Again, the easiest way will be, as I mentioned, to use just HTTP.

Pablo Santa Cruz
Well, the data has to be passed in *some* form. If you limit yourself to plain multipart/form-data and HTTP POST, there's only so much you can pass to the server.
bzlm
I'm sorry. I'm not getting it. What can't be passed with a simple HTTP-POST that you **can** pass with JSON? After all, JSON and SOAP use HTTP as transport...
Pablo Santa Cruz
@Pablo JSON can represent complex objects. By *just HTTP* I assume you mean a flat list of keys and values.
bzlm
JSON is ideally the way I want to go if possible as I can easily serialize/deserialize it on the .net side and is very lightweight.
Cragly
@Cragly I'd go with a 3rd-party JSON library. There's no shame in using tools that empower you.
bzlm
@Pablo What's wrong with JSON? You've got to convert your data to something and there isn't much that's easier to use than JSON and JSON-Framework.
kubi
@kubi there is absolutely nothing wrong with JSON. *Au contraire...*. But, you will have to add a separate library (or write your own) to handle JSON inside an iPhone App which is always somewhat annoying. OTOH, there are already APIs to handle HTTP on iPhone's SDK... That was the point I tried to make in my answer.
Pablo Santa Cruz
+4  A: 

Hi.

I'd use JSON to format the data - encoding and decoding JSON is pretty simple as this tutorial should show. Also, if the data you want to send gets more complicated, JSON will scale to match it. Sending raw data over HTTP will get confusing as your app gets more complicated without some sort of protocol. I'd steer clear of SOAP - whenever I've tried to use it it's just been big and confusing.

As for ASI, personally I like using it - it's pretty simple to get a basic connection going but can do powerful things if you need it to - see their basic documentation here.

Here's an example of ASI and JSON getting data from www.example.com and parsing it into an NSDictionary :

- (void)startConnection {
    NSURL *url = [NSURL URLWithString:@"http://www.example.com"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
    NSString *rawdata = [request responseString];
    NSDictionary* lotsOfData = [rawdata JSONValue];

    ... Do stuff with your JSON here ...
}
deanWombourne
Excellent just the sort of thing I was looking for. What about sending data the other way? Have managed to get data using the example how would I pass back my JSON to my service? So if I passed back the JSON used in the example you mentioned (edited question).
Cragly