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 (Chapter 24).

iOS, however, handles all of that behind the scenes, and makes basic networking extremely easy. To go further into networking than this chapter takes you, start with Apple’s URL Session Programming Guide. Apple also provides a generous amount of sample code.

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.

Ever since iOS 9, App Transport Security has been enforced, meaning that HTTP requests must be HTTPS requests and that the server must be using TLS 1.2 or higher. To tweak the behavior of App Transport Security, you must make an entry in your app’s Info.plist, in the ...

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