WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

Help with creating a SwiftUI app using nwConnection

Forums > SwiftUI

Hi there,

I've half succeeded in creating a SwiftUI app which allows me to connect to another piece of software via nwConnection with a host (ip address) and port.

I modified the code from this site https://rderik.com/blog/building-a-server-client-aplication-using-apple-s-network-framework/ (obviously only the client part as my server is already setup and functioning).

The server I connect to simply broadcasts variable names and values as strings which I can successfully parse and display in a list.

The problem I have is when I try to disconnect (or cancel) the connection and reconnect. I get the following errors:

nw_connection_set_state_changed_handler [C1] Cannot set state changed handler after cancel

I've tried reading the Apple documentation as well as following along line by line with the above example and although this message makes sense in a general way, I have no idea how to resolve the issue. I don't want to have to have the users close/reopen the app every time they want to close and reopen the connection.

I'm finding it difficult to find tutorials or examples for nwConnection AND SwiftUI. The majority of the SwiftUI networking tutorials all use URLSession, which as far as I can tell wouldn't be applicable to my situation.

If anyone has any tutorials/examples of networking connections (Host/Port) with SwiftUI that could be useful to me, I'd be really grateful for the link.

Thank you all for reading

   

@W6OP  

Here is a sample project I have https://github.com/W6OP/xCluster

I have a newer version I am not ready to publish yet but the networking is mostly the same. The code you would be interetested in is in TelnetManger.swift in the Network Componenets folder. On the main app the picker selection opens a connection and when a new host is chosen it closes the old connection and creates a new one. Hope that helps, let me know if it doesn't and I'll take another look at it.

   

Hi W6OP - thank you very much for the example code. I'll take some time to dig into it and report back!

   

@W6OP  

I should have explained a little more. I never call any external libraries from my views if I can help it. That is why there is a Controller.swift as an ObservableObject to pass data in both directions to/from my views. The Controller.swift makes the calls to the network and other processes in their respective files. This way I only need one @Environment or @ObservableState object and each process is in separate files to keep where a certain piece of functionality lives clear.

   

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.