Chapter 24. Basic Networking

Networking is difficult and complicated, chiefly because it’s ultimately out of your control. My motto with regard to the network is, “There’s many a slip ’twixt the cup and the lip.” You can ask for a resource from across the network, but at that point anything can happen: the resource might not be found (the server is down, perhaps), it might take a while to arrive, it might never arrive, the network itself might vanish after the resource has partially arrived. iOS, however, makes at least the basics of networking very easy, so that’s what this chapter will deal with.

To go further into networking than this chapter takes you, start with Apple’s URL Loading System Programming Guide. To go even deeper under the hood, see the CFNetwork Programming Guide. Apple also provides a generous amount of sample code. See in particular SimpleURLConnections, AdvancedURLConnections, SimpleNetworkStreams, SimpleFTPSample, and MVCNetworking.

Many earlier chapters have described interface and frameworks that network for you automatically. Put a UIWebView in your interface (Chapter 11) and poof, you’re networking; the UIWebView does all the grunt work, and it does it a lot better than you’d be likely to do it from scratch. The same is true of MPMovieViewController (Chapter 15), MFMailComposeViewController (Chapter 20), and MKMapView (Chapter 24).

HTTP Requests

In iOS 7, an HTTP request is made through an NSURLSession object. An NSURLSession is a kind of grand overarching ...

Get Programming iOS 7, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.