---
title: "nearbyConnectionData(forPeer:withCompletionHandler:)"
framework: multipeerconnectivity
role: symbol
role_heading: Instance Method
path: "multipeerconnectivity/mcsession/nearbyconnectiondata(forpeer:withcompletionhandler:)"
---

# nearbyConnectionData(forPeer:withCompletionHandler:)

Obtains connection data for the specified peer.

## Declaration

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

```swift
func nearbyConnectionData(forPeer peerID: MCPeerID) async throws -> Data
```

## 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

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: func nearbyConnectionData(forPeer peerID: MCPeerID) async throws -> Data For information about concurrency and asynchronous code in Swift, see 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 section in the overview of the MCSession class reference.

## See Also

### Managing Peers Manually

- [connectPeer(_:withNearbyConnectionData:)](multipeerconnectivity/mcsession/connectpeer(_:withnearbyconnectiondata:).md)
- [cancelConnectPeer(_:)](multipeerconnectivity/mcsession/cancelconnectpeer(_:).md)
- [connectedPeers](multipeerconnectivity/mcsession/connectedpeers.md)
