Bonjour API Architecture

OS X and iOS provide several layers of application programming interface (API) for Bonjour service applications: The NSNetService and NSNetServiceBrowser classes in the Foundation framework; CFNetServices, part of the CFNetwork framework in Core Services; DNS Service Discovery for Java (OS X only); and the low-level DNS Service Discovery API built around BSD sockets. All three API sets provide facilities for publication, discovery, and resolution of network services. Figure 3-1 illustrates the structure of the API layers. As you can see, the Multicast DNS responder (or other DNS server) sits at the lowest level, so your software does not have to interact directly with DNS.

Figure 3-1  API layers for Bonjour network services

NSNetService and NSNetServiceBrowser

The NSNetService and NSNetServiceBrowser classes, part of the Foundation framework in Cocoa, provide object-oriented abstractions for service discovery and publication. NSNetService objects represent instances of Bonjour services, either for publication or a service discovered by a client, and NSNetServiceBrowser represents a browser for a particular type of service. Most Cocoa programmers should find these classes sufficient to meet their needs. If you need more detailed control, you can use the DNS Service Discovery API from a Cocoa application.

NSNetService and NSNetServiceBrowser are scheduled on the default NSRunLoop object to perform publication, discovery, and resolution asynchronously. All results returned by NSNetService and NSNetServiceBrowser objects are handled by delegate objects. These objects must be associated with a run loop to function, but it need not be the default one.


The CFNetServices API declared in the Core Services framework provide Core Foundation-style types and functions for managing services and service discovery. CFNetServices defines three Core Foundation object types, CFNetService, CFNetServiceBrowser, and CFNetServiceMonitor. CFNetService is an abstract representation of a service instance, either for publication or for use. Associated functions provide support for publishing and resolving services. CFNetServiceBrowser represents a browser for a particular type of service in a particular domain. You should generally use this API only if you are writing code at the Core Foundation layer in OS X or iOS.

Both CFNetService and CFNetServiceBrowser objects are normally serviced in CFRunLoops. To retrieve results, applications implement callback functions to handle events, such as new services appearing or disappearing, instances being resolved, and errors occurring. Unlike NSNetService and NSNetServiceBrowser, CFNetServices types do not require a run loop and can run synchronously if this behavior is needed. However, it is bad practice to use the synchronous modes of these functions.

DNS Service Discovery

The DNS Service Discovery API, declared in /usr/include/dns_sd.h, provide low-level BSD socket communication for Bonjour services. DNS Service Discovery acts as an intermediate layer between your software and the Multicast DNS responder or DNS server. It manages the Multicast DNS responder for you, letting you write your program in terms of services and service browsers instead of DNS resource records.

Because the DNS Service Discovery API is part of the Darwin open source project, you should use it when writing cross-platform code (for platforms beyond just iOS and OS X) or when you need to use low-level features that are unavailable in higher-level APIs such as NSNetService.

DNS Service Discovery is also the API that should be used if developing Bonjour service applications for Windows, Linux, or FreeBSD.