What's New / Connectivity & Hardware

What's new in Network

+4 NewiOS · macOS · tvOS · watchOS

Network is Apple's low-level networking framework for TCP, UDP, and TLS connections. It exposes connection and listener objects plus protocol stack configuration below the URL-loading layer.

Four new APIs, no deprecations or removals. NetworkConnection and NetworkChannel are new connection types. prependApplicationProtocolIgnoringReady adds protocol-stack control, and nw_tcp_set_max_pacing_rate sets a cap on the TCP send pacing rate.

New

4
extension

NetworkChannel

NewiOSmacOStvOSwatchOS
extension NetworkChannel where ApplicationProtocol == QUICStream
Declaration
extension NetworkChannel where ApplicationProtocol == QUICStream {

    /// Set a closure to be called when the connection's path has changed, which may be called
    /// multiple times until the connection is cancelled.
    ///
    /// This closure will inherit the isolation domain of the caller.
    @discardableResult
    public func onPathUpdate(_ handler: @escaping @isolated(any) @Sendable (_ connection: Self, _ newPath: NWPath) -> Void) -> Self

    /// Set a closure to be called when the connection's viability changes, which may be called
    /// multiple times until the connection is cancelled.
    ///
    /// Connections that are not currently viable do not have a route, and packets will not be
    /// sent or received. There is a possibility that the connection will become viable
    /// again when network connectivity changes.
    ///
    /// This closure will inherit the isolation domain of the caller.
    @discardableResult
    public func onViabilityUpdate(_ handler: @escaping @isolated(any) @Sendable (_ connection: Self, _ newViable: Bool) -> Void) -> Self

    /// A better path being available indicates that the system thinks there is a preferred path or
    /// interface to use, compared to the one this connection is actively using. As an example, the
    /// connection is established over an expensive cellular interface and an unmetered Wi-Fi interface
    /// is now available.
    ///
    /// Set a closure to be called when a better path becomes available or unavailable, which may be called
    /// multiple times until the connection is cancelled.
    ///
    /// When a better path is available, if it is possible to migrate work from this connection to a new connection,
    /// create a new connection to the endpoint. Continue doing work on this connection until the new connection is
    /// ready. Once ready, transition work to the new connection and cancel this one.
    ///
    /// This closure will inherit the isolation domain of the caller.
    @discardableResult
    public func onBetterPathUpdate(_ handler: @escaping @isolated(any) @Sendable (_ connection: Self, _ newValue: Bool) -> Void) -> Self
}
extension

NetworkConnection

NewiOSmacOStvOSwatchOS
extension NetworkConnection where ApplicationProtocol : MultiplexProtocol
Declaration
extension NetworkConnection where ApplicationProtocol : MultiplexProtocol {

    /// Set a closure to be called when the connection's path has changed, which may be called
    /// multiple times until the connection is cancelled.
    ///
    /// This closure will inherit the isolation domain of the caller.
    @discardableResult
    final public func onPathUpdate(_ handler: @escaping @isolated(any) @Sendable (_ connection: NetworkConnection<ApplicationProtocol>, _ newPath: NWPath) -> Void) -> Self

    /// Set a closure to be called when the connection's viability changes, which may be called
    /// multiple times until the connection is cancelled.
    ///
    /// Connections that are not currently viable do not have a route, and packets will not be
    /// sent or received. There is a possibility that the connection will become viable
    /// again when network connectivity changes.
    ///
    /// This closure will inherit the isolation domain of the caller.
    @discardableResult
    final public func onViabilityUpdate(_ handler: @escaping @isolated(any) @Sendable (_ connection: NetworkConnection<ApplicationProtocol>, _ newViable: Bool) -> Void) -> Self

    /// A better path being available indicates that the system thinks there is a preferred path or
    /// interface to use, compared to the one this connection is actively using. As an example, the
    /// connection is established over an expensive cellular interface and an unmetered Wi-Fi interface
    /// is now available.
    ///
    /// Set a closure to be called when a better path becomes available or unavailable, which may be called
    /// multiple times until the connection is cancelled.
    ///
    /// When a better path is available, if it is possible to migrate work from this connection to a new connection,
    /// create a new connection to the endpoint. Continue doing work on this connection until the new connection is
    /// ready. Once ready, transition work to the new connection and cancel this one.
    ///
    /// This closure will inherit the isolation domain of the caller.
    @discardableResult
    final public func onBetterPathUpdate(_ handler: @escaping @isolated(any) @Sendable (_ connection: NetworkConnection<ApplicationProtocol>, _ newValue: Bool) -> Void) -> Self
}
func

nw_tcp_set_max_pacing_rate

NewiOSmacOStvOSwatchOS
public func nw_tcp_set_max_pacing_rate(_ metadata: nw_protocol_metadata_t, _ max_pacing_rate: UInt64) -> Int32
func

prependApplicationProtocolIgnoringReady

NewiOSmacOStvOSwatchOS
final public func prependApplicationProtocolIgnoringReady(options: NWProtocolOptions) throws

Dynamically add a protocol to a connection establishment attempt "above" the framer protocol. This means that the protocol above will start running once the framer becomes ready by calling markReady(). This can only be used with framers that return a value of willMarkReady to their start handlers. An example of using this functionality is adding a security protocol, like TLS, above a framer once that framer completes its initial handshake.

To ensure thread safety, this function can only be called in one of the callback blocks invoked on the framer, or in a block passed to NWProtocolFramer.Instance.async().

Throws an error if the protocol could not be added.

This function differs from prependApplicationProtocol when the framer has already been marked ready. prependApplicationProtocol will throw an error if the framer was marked ready. prependApplicationProtocolIgnoringReady will still add the protocol and reset ready. The framer must markReady again.

If there is data in-flight on the connection, the behavior is undefined.

Parameters

options
Protocol options for an application protocol to dynamically add "above" the framer.

No APIs match your filter.

← More in Connectivity & Hardware