More Naked Networking, more SwiftUI

If you come here from Naked Networking with SwiftUI you’re in the right place, if you haven’t than you need to read this first.

Naked Networking with SwiftUI

Assuming you already read it’s sister article and created the nakedNetwoking project lets plough ahead.

We will be implementing the talking part of our super simple app. It is I fear a little bit chicken and egg but bare with me. I think I’ll go the same way as the previous code. I’ll start with the network and finish with SwiftUI.

Network

Lets go back to our connect class and define the talker with this code.

We start by defining the initial connection, as with the other article you got more cases in here you need to add, but I’m focusing on naked coding.

Having made the connection we can use this method to send some data across.

Again I added a tad of less obvious error handing code into the mix.

But wait, this is where the chicken and egg come into the picture. We need to implement some SwiftUI code to test this, although I really want to test this without SwiftUI.

To keep things simple for now, lets just implement this with some nasty magic numbers and and smidge of code in SwiftUI. Note I needed to move the communication object up a level in terms of scope. Move this code you already have to above the var body.

And then tag this code into the .onAppear of the Text Button.

Now your ready to test it. Go head and run it.

You should start with a blank screen and then see the word “Push” appear when the connectToUDP runs after 4 seconds. Of course you need to make sure the magic numbers are correct. The port number should match, the IP address surely won’t.

SwiftUI

Now back into uncharted territory. What do we need. At a minimum we need a button to launch the sending of the message. Let’s do that. I’ll include all the ContentView code here cause SwiftUI is new.

What’s new from the previous version. we pushed the communication initialisation to a winder scope, introduced a vertical stack flow and moved our timed UDP message to a button. [You should delete the one that starts after 4 seconds].

Copy the code into your project and give it a go. You should initially see the just the smoke button. Press it and get “fire”.

Now it must have dawned on you that we are in fact talking to ourselves here, which like an app that simply prints ”Hello World” is of … limited value.

One of the simplest and quickest changes we can do to fix that is change the sending ip-address to a broadcasting one. Which in IP4 speak means we need to end it with a 255. Lets go one beyond that and extract the entire process into its on mini-method. within the ContentView.swift file.

Change the button action so that it now calls the new method, giving it parameters that make sense.

Now when it sends out a message, it’ll send it too all devices on the 192.168.1.X network. The network in our example.

Read on, in the next article I am going to try and build on our super simple app into a simple game.

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