views:

93

answers:

2

Hey guys, I am actually a really new programmer who is new to iphone developing and server stuff. So I have a lot of questions to ask. You don't have to answer all the questions but answering as much questions will help me a lot! thx!

  1. How does iphone apps interact with server?
  2. Is there a particular kind of server i should use to interact iphone app with server?
  3. if there is no particular kind of server then what kind of server can be used?
  4. what are their advantages and disadvantages?
  5. what should the iphone app(which is the client) do in order to interact with the server?
  6. how does the server know which iphone to send data to?
  7. what should the server do in order to interact with iphone app(client)

Thx a lot!!!

+2  A: 

Your best bet is to have your iPhone make web requests of a web server. Your iPhone app acts just like a web browser, making http requests to a web server, and parsing the response.

I'm building an app right now that hits PHP scripts I've written that do database work, etc, and return JSON objects. It's not fancy--I could have built a whole SOAP or RPC web service, but I didn't do that, it just makes GET requests with query-string arguments.

There are handy libraries you want to know about. Google "iPhone JSON" to find the JSON library written by Stig Brautaset, that's the one most people seem to be using. Also, rather than putting yourself through all the hoops that the iPhone's built-in web client framework requires, go get ASIHTTPRequest, a very powerful and MUCH simplified web client library.

As a general rule, you want to do as much processing on the server as possible. For instance, there's a place in my app I'm searching for events happening within a user-specified range of their local coordinates ("within 10 miles of me"). I wrote PHP to build a latitude/longitude bounding box, and query from the database based on that. That's WAY faster than bringing a bunch of events down and then asking Core Location to calculate their distance from where I'm standing.

Dan Ray
HTTP is a very chatty protocol. What if you need to be on-the-wire efficient?
Alan
Thanks Dan!I have a question, by using ASIHTTPRequest library, i can get data from urls right? for example, httlp://someurl.com/somephp.php. What if two iphones call that url at the same time? how does the server know which iphone is it then?Also I have another question. by using ASIHTTPRequest, in your case, you are getting JSON object from the url right? So that means you are creating the JSON object in the server side and send it to the iphone client, and then inside the client, the app will decode the JSON object and do some logic right?
David Gao
@Alan: In practice, you can probably handle the overhead of HTTP. If not, then you have access to the sockets library and you can roll your own. If you know enough to know that HTTP won't work for you, you probably know enough to know how to talk raw sockets!
Dan Ray
@David: "How does the server know which iphone it is then?" It doesn't. If you need to identify the user that's hitting you, then your first hit on the server should be a login and password. You can return some identifying data that you can keep in memory on the iPhone (or use NSUserDefaults to save between sessions). Then pass that ID as an argument on every subsequent request. Your analysis of the JSON process is dead on. I use the PHP JSON library to turn a PHP data object into a JSON message, then the JSON library on the iPhone to turn it back into a native data structure.
Dan Ray
@David: The question "how does the server know which phone it is" is just like asking "how does a web server know which copy of Firefox is making the request?" I mean, it's going to answer the request to whoever MADE the request, you don't have to worry about that part. Beyond that it doesn't know anything about the identity of any particular client.
Dan Ray
thx man! it was helpful!
David Gao
+2  A: 

You've asked quite a few questions so I'll try my best to answer them all:

First, you need to be a bit clearer, what type of server are you talking about? Email server, web server, lolcat server, it depends.

At the basic level, the iphone communicates over the internet. The internet uses Internet Protocol, and there are two standard protocols built atop of IP: Transmission Control Protocol, and User Datagram Protocol. Each has it's own uses and functions.

TCP/IP and UDP/IP make up the backbone of internet communication.

A more specific application protocol is built atop of these two internet protocols, with a specific format to a given application. For example, HTTP is the standard protocol for transferring HTML and other Web information between a web server to a web browser client, over TCP.

So, your iPhone would use whatever protocol is required to commuincate with the server. For more common server communication, the iOS SDK provides methods to construct messages (for example if you wish to make an HTTP request to a web server, you can use initWithContentsOfURL to send a GET request).

If you built a custom server, then you will need construct the required message protocol on the iphone, and send it to the server, using either TCP or UDP (whatever your custom server expects).

Alan
Thank you Alan! As for your questions, is it possible to use an email server on a app that doesn't sends email? (but rather sends data). Or is it possible to use a web server on a app that doesn't sends web data but email instead? Right now I don't even know what kind of server I should use for my iphone app...Also regarding how HTTP request would work, is it like is you use that api, initWithContentsOfURL, for example, http://someurl.com/somephp.php and then get a information, suppose there are two iphones that both calls that url, then how does the server know which iphone is it?
David Gao
Well you wouldn't use a server on an app (a server handles client connections and handles service requests from clients--your app would likely be a client, not a server). And in general if you have a web server, it needs to handle web data, otherwise it's not a webserver, but just a server of some type. THe type of requests a server handles dictates it's type. Re: Web requests, if you need your server to know which iphone, you must pass along that information in the request. You can use query strings: `someurl.com/somephp.php?id=davids_iphone` or pass as part of the HTTP auth headers
Alan
Before I get ahead of myself, ask, why does your server need to know what iphone is requesting the page? Who is requesting a resource is typically handled by "authentication" and there are a number of ways to go about authenticating a web session.
Alan
I think I get a lot more about how iphone interacts with server. thx man!
David Gao