Naked Networking with SwiftUI

Network

Back in the dark ages when I entered the world of computing the way to get two computers to talk to each other was to use black magic, also known as sockets.

The techs at Apple who are I must admit are pretty good at developing frameworks decided to put their mind to bringing sockets up to date. They did so with the Network framework, published in 2018.

I hope in this article to go thru a super simple socket implementation. I also hope to dip into SwiftUI while I do so too. I like a challenge.

Where to start. Ok there are two flavours, TCP and UDP. In short TCP sockets guarantee delivery, UDP sockets do not. TCP are useful when you need to be sure the message you sent arrived. UDP sockets are useful when you more concerned about speed.

I am going to focus on UDP for this article, cause they are a tad easier to implement :) and maybe more interesting to the reader, since they are sort of sockets you want to use for that killer game app you been meaning to develop.

Now, as with all conversations we have somebody talking and somebody listening. A socket pair in socketese. On one device you listen and on the other you talk.

Lets start by creating a project, call it nakedNetworking, why not. I am using iOS 13, Swift 5 and Xcode 11.3.1.

Create the project and go with the Hello World template you get when you select SwiftUI interface. Next create a separate file for our Network code, a class we’ll call Connect. Here is the code for that.

So we got a listener [listening] and a talker [talking]. of course there are more cases than I included here, you should add them. We need to focus on naked here.

The eagle eyed readers may have noticed a that there is a method tucked into the code here that isn’t defined. I defined it here.

I added the error check here cause it not so obvious to the uninitiated reader, strictly speaking not naked coding.

You almost have the bare bones now, you just need to start listening. You do so in the View by adding this code ContentView.swift.

You should now be able to compile and run it. On screen you’ll get the words “Hello World”, which is absolutely useless but, but useful your networking code will also run in the background. To test it you’ll need to send something to the UDP socket you’re listening on. Run this command on a terminal to do so.

You don’t need to worry about all the flags here, just that the IP address [which will need to be your device or computer if you’re running on a simulator and the port number “1984” that comes from the code see above].

Run your app and try the echo. You should see the word “peek” appear on the debug screen.

SwiftUI

Ok, I know what you’re thinking. An app that outputs things to the debug consoles isn’t real useful, what about SwiftUI.

Apple introduced SwiftUI in 2019, when they did so they showed a number of interesting ways to connect things together, although sadly in my research for this article I haven’t found this method for doing what I want to here, so be warned we’re going to be flying by our pants here.

The plan is to output the data send to the label. We can start with the Connect class. Let’s create another class within it and publish a variable thru it.

The @Published is a new class of tag that appeared in SwiftUI. Next we need to replace our print statement in the receive method on the Connect class to set the value of our score variable we just defined. Note I used a singleton for the score, cause in the words of SwiftUI manta I want a single source of truth :)

Note too that I run this on the main thread, it is important to do so since this statement will in effect by updating the display.

We’re almost done. Now we just need to make a few small changes to the ContentView class, which I include a full copy of here to keep things simple.

Now if you run that echo again you’ll see the word you just sent appear on the screen of your device. Try a different note say “peek” or a “pop”. And that’s it with a peek, poke and pop I close article.

Although just before I go, you’re right we did a half baked job on the networking code, where is talking you may rightly ask. That will be in the next naked Networking installment.

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