iPhone Networking

2020-08-05 10:30发布

问题:

If I have make an application, how would I make it interact with a database on a server of mine? I'm kind of lost, and don't know where to start.

回答1:

You might try Apple's "Getting Started with Networking & Internet" or "Introduction to the URL Loading System", in addition to the Networking section of the iPhone Application Programming Guide. There are also several sample applications that handle networking of various types within the ADC's iPhone section.

Additionally, there are a number of open source iPhone applications out there that handle networking, including my own.



回答2:

Vague question yields a vague answer.

It entirely depends on what kind of interaction you need. Lots of constant queries? Few and far between? Client side cache? Real time updates? All of these questions will impact the answer.

The easiest way is to go with an AJAX style HTTP based client/server type of interaction. Sticking a database behind a web server has been done about a bazillion times and, thus, you'll find lots and lots of examples and, even, solutions with a few google searches.

You don't really need to use JavaScript (the J in AJAX). Instead, send over an HTTP request that encapsulates your query and have the server respond with an XML document containing the answer.

If that won't work for you -- too much overhead, need to relay binary information (for which XML sucks), etc.. -- then you'll wan to go with more direct access to the database. That is harder and can range from porting a client library to the iPhone to creating your own wire protocol.

A significantly harder problem and you'll have to deal with networking issues -- firewalls, NAT, proxies, etc... -- that are generally already solved with HTTP.

Stick with HTTP until you prove that it won't work. Much simpler.

Search for "http request" in the iPhone docs. HTTP client APIs are included.



回答3:

I had a similar question regarding a rails app - the answers there may help you.

What is the best approach for building an iphone client for a rails app?

But the answer really depends on your knowledge of iphone programming and server side programming, plus how your database is set up at the moment - but most likely you're going to need to write some kind of webservice / REST API that allows a remote client to do things with your database.

There are many frameworks available for that on the server side - if you're starting from scratch, ruby on rails may be a good choice.

On the iphone side, you'll probably want to start by reading up on NSURLConnection, and various request/response formats that you can use with it. I've found exchanging plists between the phone and server to be a pretty easy approach.