If you want to minimize your coding, you can get a quick HTTP reply from your server with a method call like this:
NSURL* url = [NSURL URLWithString:@"http://mysite.com/my_page.html"];
NSStringEncoding encoding;
NSError* error = nil;
NSString* pageData = [NSString stringWithContentsOfURL:url
usedEncoding:&encoding error:&error];
// Now pageData is a string with the html from that URL, or error will indicate
// any network error that occurred.
NSData
has a similar method called dataWithContentsOfURL:options:error:
which can handle getting binary data from a server. Both of these approaches are synchronous, meaning your code becomes blocked while it is awaiting a response from the server - at least until the timeout hits, or an error is detected.
For asynchronous network communications, you can use other methods in NSURLConnection
, which also works with companion classes NSURLRequest
, NSURLResponse
and NSURL
. The quickest way to learn this is to glance through the NSURLConnection
docs. Here's some example code of how to write an asynchronous HTTP get using these classes.
I'm assuming you have mainly HTTP transfers in mind, for which the above classes can handle
most interactions, including different HTTP request types (e.g. post vs get), different encoding types or binary data, allowing your app to handle each packet as it arrives, hooking into http-level redirects, setting a custom timeout, etc.
There are still more ways to communicate, such as using Bonjour, which assists with server-less setups (such as two iPhones sharing a wi-fi connection); or Game Kit, which can handle peer-to-peer bluetooth connections, and has support designed for in-game voice communications.