<!--
{
  "availability" : [
    "iOS: 9.0.0 - 18.0.0",
    "iPadOS: 9.0.0 - 18.0.0",
    "macCatalyst: 13.1.0 - 18.0.0",
    "macOS: 10.11.0 - 15.0.0",
    "tvOS: 17.0.0 - 18.0.0",
    "visionOS: 1.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "NetworkExtension",
  "identifier" : "/documentation/NetworkExtension/NWUDPSession/writeDatagram(_:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Network Extension"
    ],
    "preciseIdentifier" : "c:objc(cs)NWUDPSession(im)writeDatagram:completionHandler:"
  },
  "title" : "writeDatagram(_:completionHandler:)"
}
-->

# writeDatagram(_:completionHandler:)

Write a single datagram.

```
func writeDatagram(_ datagram: Data, completionHandler: @escaping ((any Error)?) -> Void)
```

## Parameters

`datagram`

An <doc://com.apple.documentation/documentation/Foundation/NSData> object containing the datagram to write. To improve performance, this should be an immutable data object.

`completionHandler`

A handler called when the write request has either succeeded or failed.

## Discussion

Callers should wait until the `completionHandler` is executed before issuing another write.

---

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)