Naked Networking Not with Bonjour and SwiftUI

We have over the past six articles developed a simple game of ping/pong, you need to read these articles for the full story.

Naked Networking with SwiftUI
More Naked Networking, more SwiftUI
Naked Networking and SwiftUI, the game plan
Naked Networking and SwiftUI, gamification
Naked Networking and SwiftUI, gamification II
Naked Networking and SwiftUI, gamification III

But we cheated just a little in that we used naked network sockets, hardcoded ones with IP addresses and port numbers. Now it would be possible to get your users to find enter this information in some text fields in your game should you want to ship it to production, but it isn’t very user friendly for 2020. It’s good to know the building blocks behind these things, but today people want more, so lets give them more with Bonjour.

But wait before I launch into code a few words about Bonjour. If you’re from the dark ages like me you will no doubt be a little confused by this protocol initially, looking for ip addresses and port numbers… you won’t find any, but don’t worry cause you don’t need them.


Now to change our app. We start in the Network file Connect.swift. with the listeningUDP method, which gains a parameter and a new line shown here in bold italic.

Our code above also has an additional method so that we can ensure things are running, the service registration handler. The next method to change is connectToUDP that also gains a parameter and a slightly different NWConnection call.

Both receive and send methods remain unchanged. Of course once you make these changes your contentView.swift is going to start complaining. We need to make a quite a few changes there too.


The ToggleModel will need to change, basically instead of using port numbers we will use bonjour services with different names. The new struct looks like this.

Can’t help thinking that this code is actually a little easier to understand. Next we need to change the call to listenUDP under the .onAppear above the pingPublisher, shown here in bold italic.

We also need to change the makeConnect method and indeed all the calls too it. The new method looks like this.

It is called 8 times throughout the ContentView.swift code base. The places it is called remains the same. All you need to do change the calls to look like this pair. Changed code in bold italic, obviously you don’t change the different messages that would be sent, just the first parameter.

You need to change parameter port:1984 to zeus: “pong” and port: 8491 to zeus: “ping”.

A side note, to help you debug your app, you can use the command shown in bold here. A command that will show your bonjour service if found.

% dns-sd -B _whack._udp
Browsing for _whack._udp
DATE: ---Tue 04 Feb 2020---
19:13:13.989 ...STARTING...
Timestamp A/R Flags if Domain Service Type Instance Name
19:13:13.989 Add 3 21 local. _whack._udp. pong
19:13:13.989 Add 3 4 local. _whack._udp. pong
19:13:18.968 Add 2 5 local. _whack._udp. ping
19:13:19.068 Add 2 4 local. _whack._udp. ping

And there you have it. Make the changes and test your app.

Finally one of drawbacks we now face however is that previously we could make play a game between multiple players. Our setup now looks more like a game for just two.

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store