If you’re looking for general guidance on Bonjour, check out the Bonjour Overview.
The key thing with Bonjour is that you shouldn’t think about networks but instead about services. Your app does not set up a peer-to-peer network, rather it publishes a service and various clients can browse for and connect to that service. In this design, Bonjour does not provide the actual networking — you can use any general-purpose networking API you like for that — but rather it providers the service discovery and advertising.
To answer your specific questions:
1) Any particularities about where I should set
includesPeerToPeer
?
Generally you know in advance whether you want to support peer-to-peer networking, in which case you should set this on your browser or service object immediately after creating it.
2) Should I be using 'initWithDomain:type:name' to initialize my p2p network? If so, what domain should I be using?
This question doesn’t make sense. In Bonjour you don’t “initialise a p2p network”, you register a service. If you set
includesPeerToPeer
, that service is discoverable over peer-to-peer networks.
Similarly, when you browser for or connect to a service, setting
includesPeerToPeer
allows those operations to occur over peer-to-peer networks.
3) How do I detect whether or not there exists a receiver and if so, deliver content to them?
There’s two common approaches for this:
The first time you connect you use a browser object (
NSNetServiceBrowser
) to search for services of the right type. You present that list to the user, who can then choose their service.Subsequently you can connect directly to that service (in Bonjour parlance this is the resolve operation, but I generally recommend that you a resolve-then-connect API).
However, Bonjour gives you a lot of flexibility here. For example:
If you never want to remember a service, you can present the browser every time.
If you’ve discovered the name and domain of a service via other means, you can connect without ever showing a browser.
Any example code/resources you might be able to recommend towards this high-level goal is much appreciated.
A good place to start is the WiTap sample code.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"