Chapter 14. Networking

Many chapters in books like this begin with something like, “The ability to talk to computers over the network is an increasingly popular feature.” We won’t bore you with that. Suffice to say—it’s the 21st century, networking is huge, and your app needs to send and receive data. Let’s learn how.

In this chapter, you’ll learn how to make connections over the network and access resources with URLs. You’ll also learn how to use Bonjour to discover nearby network services so that you can connect to them. Finally, you’ll learn how to create your own network service and receive connections from other devices.

All of the content in this chapter applies to both OS X and iOS.

Connections

At the lowest level, network connections in Cocoa are the same as in every other popular OS. The Berkeley sockets API, the fundamental networking and connectivity API used on Windows and Unix OSes (which includes OS X and iOS), is available, allowing you to make connections to any computer on the network and send and receive data.

However, working with such a low-level API can be cumbersome, especially when you want to use popular, higher level protocols like HTTP. To make things more fun for developers like us, Cocoa provides a higher level API that provides a simple interface for accessing content via URLs on the Internet.

Note

A URL is a uniform resource locator. It’s a location on the Internet, and specifies the location of the server to connect to, the protocol to use, and the location ...

Get Swift Development with Cocoa 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.