How to close / cancel a NetworkConnection

Hello,

I have an app that was using the iOS 18 Network Framework APIs. It used Peer to Peer, QUIC and Bonjour. It was all working as expected. I wanted to upgrade to the new iOS 26 Network Framework APIs (NetworkBrowser, NetworkListener, NetworkConnection...).

I have things working (multiple devices can discover each other, connection to each other and send messages to each other) but my app crashes when I go to toggle of all the networking stuff.

In the iOS 18 Network Framework API NWConnection had a .cancel() function I could use to tell the other side the connection was done.

I dont see a cancel function for NetworkConnection.

My question is - how do I properly close down a NetworkConnection and also properly tell the other side the connection is done.

There’s no explicit cancel method in the new API. Rather, our expectation is that you’d use structure concurrency, meaning that:

  • All the use of a given connection would be within one scope.
  • When you exit that scope, that drops the last reference to the connection.
  • Which then cancels it.

There’s a similar setup for listeners and browser, except those enforce the rules by means of their run(…) methods.

my app crashes when I go to toggle of all the networking stuff.

Can you share an example crash report? See Posting a Crash Report for advice on how to do that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to close / cancel a NetworkConnection
 
 
Q