开发者

Whats the best way to have an iPhone app to pass data to a website?

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" }

]

}


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 ...
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜