<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MultipeerConnectivity",
  "identifier" : "/documentation/MultipeerConnectivity/MCSession/nearbyConnectionData(forPeer:withCompletionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Multipeer Connectivity",
      "MultipeerConnectivity"
    ],
    "preciseIdentifier" : "c:objc(cs)MCSession(im)nearbyConnectionDataForPeer:withCompletionHandler:"
  },
  "title" : "nearbyConnectionData(forPeer:withCompletionHandler:)"
}
-->

# nearbyConnectionData(forPeer:withCompletionHandler:)

Obtains connection data for the specified peer.

```
func nearbyConnectionData(forPeer peerID: MCPeerID, withCompletionHandler completionHandler: @escaping @Sendable (Data?, (any Error)?) -> Void)
```

## Parameters

`peerID`

A peer ID object obtained from the nearby peer that you want to add to a session.

`completionHandler`

A handler that is called when connection data is available or when an error occurs.

## Discussion

> Important:
> You can call this method from synchronous code using a completion handler, as shown on this page, or you can call it as an asynchronous method that has the following declaration:
> 
> ```swift
> func nearbyConnectionData(forPeer peerID: MCPeerID) async throws -> Data
> ```
> 
> For information about concurrency and asynchronous code in Swift, see <doc://com.apple.documentation/documentation/Swift/calling-objective-c-apis-asynchronously>.

This method provides connection data that is required when adding a specific nearby peer to a session if you are using your own service discovery code instead of an `MCNearbyServiceBrowser` or `MCBrowserViewController` object. For more information, see the [`Managing Peers Manually`](/documentation/MultipeerConnectivity/MCSession#Managing-Peers-Manually) section in the overview of the `MCSession` class reference.

---

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)