Chapter 24. Basic Networking

Networking is difficult and complicated, not least because it’s ultimately out of your control. You can ask for a resource from across the network, but at that point anything can happen. The resource might not be found; it might take a while to arrive; it might never arrive. The server or the network might be unavailable, or even worse, might vanish after the resource has partially arrived. There are numerous technicalities to deal with, not to mention the need for extensive background threading so that nothing interferes with the operation of your app’s interface. Fortunately, iOS handles all of that behind the scenes, and makes basic networking easy.

Earlier chapters have described interface and frameworks that network for you automatically. Put a web view in your interface (Chapter 12) and poof, you’re networking; the web view 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 AVPlayer (Chapter 16), MKMapView (Chapter 21), and so on. Think of that as implicit networking. This chapter discusses explicit networking.

Tip

A device used for development has a Network Link Conditioner switch in Settings (under Developer). Use it to simulate different networking situations to stress-test your networking code.

HTTP Requests

An HTTP request is made through a URLSession object. A URLSession is a kind of grand overarching environment in which network-related tasks are to take place.

Obtaining ...

Get Programming iOS 14 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.