Structured Concurrency with Network Framework Sample

I am trying to migrate an app to use Network framework for p2p connection. I came across this great article for migrating to Network framework however this doesnt use the new structured concurrency. This being introduced with iOS 26, there doesnt seem to be any sample code available on how to use the new classes. I am particularly interested in code samples showing how to add TLS with PSK encryption support and handling of switching between Wifi and peer to peer interface with the new structured concurrency supported classes. Are there any good resources I can refer on this other than the WWDC video?

Answered by DTS Engineer in 866929022
this doesnt use the new structured concurrency.

Right. It was written before the new API was release and I haven’t updated it yet because most folks who want to do this need to support iOS 18, where the new API isn’t available. Oh, and I haven’t had time |-:

I am particularly interested … TLS with PSK encryption support

AFAIK that’s not currently supported (r. 159170556)-:.

I’d appreciate you filing an enhancement request for that. While we have a bug about it already, it’s an internal bug, and bugs from third-party developers can help in situations like this.

Please post your bug number, just for the record.

switching between Wifi and peer to peer interface

I’m not sure I understand that. Please elaborate.

Share and Enjoy

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

this doesnt use the new structured concurrency.

Right. It was written before the new API was release and I haven’t updated it yet because most folks who want to do this need to support iOS 18, where the new API isn’t available. Oh, and I haven’t had time |-:

I am particularly interested … TLS with PSK encryption support

AFAIK that’s not currently supported (r. 159170556)-:.

I’d appreciate you filing an enhancement request for that. While we have a bug about it already, it’s an internal bug, and bugs from third-party developers can help in situations like this.

Please post your bug number, just for the record.

switching between Wifi and peer to peer interface

I’m not sure I understand that. Please elaborate.

Share and Enjoy

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

AFAIK that’s not currently supported (r. 159170556)

I would prefer to use an encrypted channel for communication. What options do I have with NetworkConnection in that case?

For the switching from Wifi and Peer to Peer part, I was curious if there was a guide on best practices for usage of onBetterPathUpdate and onViabilityUpdate for switching from an unstable interface to a better available interface. There doesnt seem to be much documentation on how these functions are supposed to be used.

What options do I have with NetworkConnection in that case?

Use TLS-PKI.

To start you out, lemme point you at two posts:

The first defines key terminology. The second offers some suggestions for how to deploy TLS in local environment. While TLS-PSK is an option in that context, it’s not required. Indeed, most of that post covers options that use TLS-PKI.


Regarding the better path and viability handlers, these are conceptually the same as the equivalent constructs in the old APIs, so you can look for information about those old APIs and map that to this API.

The better path handler is typically not necessary in a peer-to-peer context. It’s more commonly used to handle the switch between WWAN and Wi-Fi. See Extra-ordinary Networking > Network Interface Techniques > A Better Path.

OTOH, a viability handler is a good idea in most cases. I actually have a pretty good summary of what that’s for and what to do it in Moving from Multipeer Connectivity to Network Framework.

Share and Enjoy

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

Use TLS-PKI.

Are you suggesting to do this at the application layer? The NetworkConnection/ NetworkListener classes doesnt seem to support any custom TLS configuration using NWParameters.

To start you out, lemme point you at two posts:

Thank you! These were very helpful.


The better path handler is typically not necessary in a peer-to-peer context.

Understood. To add more context here, the communication I am trying to establish here is between two iOS devices. The multipeer connectivity framework used to switch from Infrastructure Wifi to Peer to Peer Wifi automatically when the Wifi went down. I'm trying to mimic that behaviour with Nework framework. The migration guide briefly touches upon this topic to set includePeerToPeer to enable peer to peer wifi but it isnt clear if that will mimic the behavior of Multipeer.

The [new] classes doesnt seem to support any custom TLS configuration using NWParameters.

With the new API you generally configure stuff using modifiers. For example, here’s how to do custom server trust evaluation on a connection:

let endpoint: NWEndpoint = endpoint
let connection = NetworkConnection(to: endpoint) {
    TLS()
        .certificateValidator { _, trust in
            let shouldConnectToServer = … check trust …
            return shouldConnectToServer
        }
}

The multipeer connectivity framework used to switch from Infrastructure Wifi to Peer to Peer Wifi automatically when the Wifi went down.

It did? I find that quite surprising? How did you actually test that?

Share and Enjoy

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

Structured Concurrency with Network Framework Sample
 
 
Q