UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

3      

@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.

3      

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

3      

@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.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.