Chapter 23. 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. iOS, however, handles all of that behind the scenes, and makes basic networking extremely easy.
Many earlier chapters have described interface and frameworks that network for you automatically. Put a web view in your interface (Chapter 11) 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 15), MKMapView (Chapter 20), 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. ...
Get Programming iOS 12 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.