<!--
{
  "availability" : [
    "iOS: 12.0.0 -",
    "iPadOS: 12.0.0 -",
    "macCatalyst: 12.0.0 -",
    "macOS: 10.14.0 -",
    "tvOS: 12.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 5.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Network",
  "identifier" : "/documentation/Network/NWConnection",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Network"
    ],
    "preciseIdentifier" : "s:7Network12NWConnectionC"
  },
  "title" : "NWConnection"
}
-->

# NWConnection

A bidirectional data connection between a local endpoint and a remote endpoint.

```
final class NWConnection
```

## Topics

### Creating Connections

[`init(host:port:using:)`](/documentation/Network/NWConnection/init(host:port:using:))

Initializes a new connection to a host and port.

[`init(to:using:)`](/documentation/Network/NWConnection/init(to:using:))

Initializes a new connection to a remote endpoint.

[`start(queue:)`](/documentation/Network/NWConnection/start(queue:))

Starts establishing a connection, and sets the queue on which to deliver all connection events.

[`restart()`](/documentation/Network/NWConnection/restart())

Restarts a connection that is in the waiting state.

### Handling State Updates

[`state`](/documentation/Network/NWConnection/state-swift.property)

The current state of the connection.

[`NWConnection.State`](/documentation/Network/NWConnection/State-swift.enum)

States indicating whether a connection can be used to send and receive data.

[`stateUpdateHandler`](/documentation/Network/NWConnection/stateUpdateHandler)

A handler that receives connection state updates.

### Sending and Receiving Data

[`send(content:contentContext:isComplete:completion:)`](/documentation/Network/NWConnection/send(content:contentContext:isComplete:completion:)-5ecuz)

Sends data on a connection.

[`send(content:contentContext:isComplete:completion:)`](/documentation/Network/NWConnection/send(content:contentContext:isComplete:completion:)-3mfmt)

Sends data on a connection using a custom Data type.

[`NWConnection.SendCompletion`](/documentation/Network/NWConnection/SendCompletion)

A completion handler that indicates when the connection has finished processing sent content.

[`receive(minimumIncompleteLength:maximumLength:completion:)`](/documentation/Network/NWConnection/receive(minimumIncompleteLength:maximumLength:completion:))

Schedules a single receive completion handler, with a range indicating how many bytes the handler can receive at one time.

[`receiveMessage(completion:)`](/documentation/Network/NWConnection/receiveMessage(completion:))

Schedules a single receive completion handler for a complete message, as opposed to a range of bytes.

[`batch(_:)`](/documentation/Network/NWConnection/batch(_:))

Defines a block in which calls to send and receive are processed as a batch to improve performance.

[`NWConnection.ContentContext`](/documentation/Network/NWConnection/ContentContext)

An object that represents a message to send or receive, containing protocol metadata and send properties.

[`maximumDatagramSize`](/documentation/Network/NWConnection/maximumDatagramSize)

The maximum size of a datagram message that can be sent on a connection.

### Canceling Connections

[`cancel()`](/documentation/Network/NWConnection/cancel())

Cancels the connection and gracefully disconnects any established network protocols.

[`forceCancel()`](/documentation/Network/NWConnection/forceCancel())

Cancels the connection and immediately disconnects any established network protocols.

[`cancelCurrentEndpoint()`](/documentation/Network/NWConnection/cancelCurrentEndpoint())

Causes the current endpoint to be rejected, allowing the connection to try another resolved address.

### Handling Path Updates

[`currentPath`](/documentation/Network/NWConnection/currentPath)

The network path the connection is using.

[`pathUpdateHandler`](/documentation/Network/NWConnection/pathUpdateHandler)

A handler that receives network path updates.

[`viabilityUpdateHandler`](/documentation/Network/NWConnection/viabilityUpdateHandler)

A handler that receives updates when data can be sent and received.

[`betterPathUpdateHandler`](/documentation/Network/NWConnection/betterPathUpdateHandler)

A handler that receives updates when an alternative network path is preferred over the current path.

### Collecting Connection Metrics

[Collecting Network Connection Metrics](/documentation/Network/collecting-network-connection-metrics)

Use reports to understand how DNS and protocol handshakes impact
connection establishment.

[`requestEstablishmentReport(queue:completion:)`](/documentation/Network/NWConnection/requestEstablishmentReport(queue:completion:))

Requests a copy of the connection’s establishment report once the connection is in the ready state.

[`NWConnection.EstablishmentReport`](/documentation/Network/NWConnection/EstablishmentReport)

A report that provides metrics about the establishment of a connection.

[`startDataTransferReport()`](/documentation/Network/NWConnection/startDataTransferReport())

Begins a new data transfer report, which can later be collected.

[`NWConnection.PendingDataTransferReport`](/documentation/Network/NWConnection/PendingDataTransferReport)

An outstanding data transfer report that has yet to be collected.

[`NWConnection.DataTransferReport`](/documentation/Network/NWConnection/DataTransferReport)

A report that provides metrics about data being sent and received on a connection.

### Inspecting Connections

[`metadata(definition:)`](/documentation/Network/NWConnection/metadata(definition:))

Retrieves the connection-wide metadata for a specific protocol.

[`NWProtocolMetadata`](/documentation/Network/NWProtocolMetadata)

The abstract superclass for specifying metadata about a network protocol.

[`endpoint`](/documentation/Network/NWConnection/endpoint)

The remote endpoint with which the connection was initialized.

[`parameters`](/documentation/Network/NWConnection/parameters)

The parameters with which the connection was initialized.

[`queue`](/documentation/Network/NWConnection/queue)

The queue on which connection events are delivered.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)