A bidirectional data connection between a local endpoint and a remote endpoint.
SDKs
- iOS 12.0+
- macOS 10.14+
- Mac Catalyst 13.0+
- tvOS 12.0+
- watchOS 5.0+
- Xcode 10.0+
Framework
- Network
Declaration
final class NWConnection
A bidirectional data connection between a local endpoint and a remote endpoint.
SDKs
Framework
final class NWConnection
init(host: NWEndpoint .Host, port: NWEndpoint .Port, using: NWParameters)
Initializes a new connection to a host and port.
init(to: NWEndpoint, using: NWParameters)
Initializes a new connection to a remote endpoint.
func start(queue: Dispatch Queue)
Starts establishing a connection, and sets the queue on which to deliver all connection events.
func restart()
Restarts a connection that is in the waiting state.
var state: NWConnection .State
The current state of the connection.
enum NWConnection .State
States indicating whether a connection can be used to send and receive data.
var state Update Handler: ((NWConnection .State) -> Void)?
A handler that receives connection state updates.
enum NWConnection .Send Completion
A completion handler that indicates when the connection has finished processing sent content.
func receive(minimum Incomplete Length: Int, maximum Length: Int, completion: (Data?, NWConnection .Content Context?, Bool, NWError?) -> Void)
Schedules a single receive completion handler, with a range indicating how many bytes the handler can receive at one time.
func receive Message(completion: (Data?, NWConnection .Content Context?, Bool, NWError?) -> Void)
Schedules a single receive completion handler for a complete message, as opposed to a range of bytes.
func batch(() -> Void)
Defines a block in which calls to send and receive are processed as a batch to improve performance.
class NWConnection .Content Context
An object that represents a message to send or receive, containing protocol metadata and send properties.
var maximum Datagram Size: Int
The maximum size of a datagram message that can be sent on a connection.
func cancel()
Cancels the connection and gracefully disconnects any established network protocols.
func force Cancel()
Cancels the connection and immediately disconnects any established network protocols.
func cancel Current Endpoint()
Causes the current endpoint to be rejected, allowing the connection to try another resolved address.
var current Path: NWPath?
The network path the connection is using.
var path Update Handler: ((NWPath) -> Void)?
A handler that receives network path updates.
var viability Update Handler: ((Bool) -> Void)?
A handler that receives updates when data can be sent and received.
var better Path Update Handler: ((Bool) -> Void)?
A handler that receives updates when an alternative network path is preferred over the current path.
Use reports to understand how DNS and protocol handshakes impact connection establishment.
func request Establishment Report(queue: Dispatch Queue, completion: (NWConnection .Establishment Report?) -> Void)
Requests a copy of the connection's establishment report once the connection is in the ready state.
struct NWConnection .Establishment Report
A report that provides metrics about how a connection was established.
func start Data Transfer Report() -> NWConnection .Pending Data Transfer Report
Begins a new data transfer report, which can later be collected.
class NWConnection .Pending Data Transfer Report
An outstanding data transfer report that has yet to be collected.
struct NWConnection .Data Transfer Report
A report that provides metrics about data being sent and received on a connection.
func metadata(definition: NWProtocol Definition) -> NWProtocol Metadata?
Retrieves the connection-wide metadata for a specific protocol.
class NWProtocol Metadata
The abstract superclass for specifying metadata about a network protocol.
let endpoint: NWEndpoint
The remote endpoint with which the connection was initialized.
let parameters: NWParameters
The parameters with which the connection was initialized.
var queue: Dispatch Queue?
The queue on which connection events are delivered.
var debug Description: String
The description of the connection.
class NWListener
An object you use to listen for incoming network connections.
class NWBrowser
An object you use to browse for available network services.
class NWEthernet Channel
An object you use to send and receive custom Ethernet frames.