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

# MCSession

An `MCSession` object enables and manages communication among all peers in a Multipeer Connectivity session.

```
class MCSession
```

## Overview

### Initiating a Session

To set up a session:

1. Use the [`init(displayName:)`](/documentation/MultipeerConnectivity/MCPeerID/init(displayName:)) method of the [`MCPeerID`](/documentation/MultipeerConnectivity/MCPeerID) to create a peer ID that represents the local peer, or retrieve a peer ID that you previously archived (to maintain a stable peer ID over time).
2. Use the peer ID with the method [`init(peer:)`](/documentation/MultipeerConnectivity/MCSession/init(peer:)) to initialize the session object.
3. Invite peers to join the session using an [`MCNearbyServiceBrowser`](/documentation/MultipeerConnectivity/MCNearbyServiceBrowser) object, an [`MCBrowserViewController`](/documentation/MultipeerConnectivity/MCBrowserViewController) object, or your own peer discovery code. (Sessions currently support up to 8 peers, including the local peer.)
4. Set up an [`MCNearbyServiceAdvertiser`](/documentation/MultipeerConnectivity/MCNearbyServiceAdvertiser) object or [`MCAdvertiserAssistant`](/documentation/MultipeerConnectivity/MCAdvertiserAssistant) object to allow other devices to ask your app to join a session that they create.

If you use one of the framework’s browser objects for peer discovery, when a peer accepts an invitation, the session calls its delegate object’s [`session(_:peer:didChange:)`](/documentation/MultipeerConnectivity/MCSessionDelegate/session(_:peer:didChange:)) method with [`MCSessionState.connected`](/documentation/MultipeerConnectivity/MCSessionState/connected) as the new state, along with an object that tells you which peer became connected. See Creating a Session for related methods.

If instead you write your own peer discovery code, you are responsible for managing the connection manually. See the [`Managing Peers Manually`](/documentation/MultipeerConnectivity/MCSession#Managing-Peers-Manually) section for more information.

### Communicating with Peers

Once you have set up the session, your app can send data to other peers by calling one of the following methods, found in Sending Data and Resources:

- [`send(_:toPeers:with:)`](/documentation/MultipeerConnectivity/MCSession/send(_:toPeers:with:)) sends an `NSData` object to the specified peers.
  
  On each recipient device, the delegate object’s [`session(_:didReceive:fromPeer:)`](/documentation/MultipeerConnectivity/MCSessionDelegate/session(_:didReceive:fromPeer:)) method is called with the data object when the data has been fully received.
- [`sendResource(at:withName:toPeer:withCompletionHandler:)`](/documentation/MultipeerConnectivity/MCSession/sendResource(at:withName:toPeer:withCompletionHandler:)) sends the contents from an `NSURL` object to the specified peer. The URL can be either a local file URL or a web URL. The `completionHandler` block is called when the resource is fully received by the recipient peer or when an error occurs during transmission.
  
  This method returns an `NSProgress` object that you can use to cancel the transfer or check the current status of the transfer.
  
  On the recipient device, the session calls its delegate object’s [`session(_:didStartReceivingResourceWithName:fromPeer:with:)`](/documentation/MultipeerConnectivity/MCSessionDelegate/session(_:didStartReceivingResourceWithName:fromPeer:with:)) method when the device begins receiving the resource, and calls its [`session(_:didFinishReceivingResourceWithName:fromPeer:at:withError:)`](/documentation/MultipeerConnectivity/MCSessionDelegate/session(_:didFinishReceivingResourceWithName:fromPeer:at:withError:)) method when the resource has been fully received or when an error occurs.
- [`startStream(withName:toPeer:)`](/documentation/MultipeerConnectivity/MCSession/startStream(withName:toPeer:)) creates a connected byte stream (`NSOutputStream`) that you can use to send data to the specified peer.
  
  On the recipient device, the session calls its delegate object’s [`session(_:didReceive:withName:fromPeer:)`](/documentation/MultipeerConnectivity/MCSessionDelegate/session(_:didReceive:withName:fromPeer:)) method with an `NSInputStream` object that represents the other endpoint of communication.
  
  On both sides, your code must set the stream’s delegate, schedule the stream on a run loop, and open the stream. Your code must also implement stream delegate methods to manage sending and receiving stream data.

> Important:
> Delegate calls occur on a private operation queue. If your app needs to perform an action on a particular run loop or operation queue, its delegate method should explicitly dispatch or schedule that work.

### Managing Peers Manually

If instead of using the framework’s browser and advertiser objects to perform peer discovery, you decide to write your own peer discovery code (with `NSNetService` or the Bonjour C API, for example), you can manually connect nearby peers into a session. To do this:

1. Establish a connection from your app to nearby peers, and exchange peer IDs with those peers.

Each peer should serialize its own local `MCPeerID` object with `NSKeyedArchiver`, and the receiving peer should unserialize it with `NSKeyedUnarchiver`.

> Important:
> Do not attempt to construct a peer ID object for a nonlocal peer using ``doc://com.apple.multipeerconnectivity/documentation/MultipeerConnectivity/MCPeerID/init(displayName:)``. A peer ID object must be constructed *on the device that it represents*.

1. Exchange connection data. After you have obtained the nearby peer’s ID object, call [`nearbyConnectionData(forPeer:withCompletionHandler:)`](/documentation/MultipeerConnectivity/MCSession/nearbyConnectionData(forPeer:withCompletionHandler:)) to obtain a connection data object specific to that nearby peer.

When the completion handler block is called, send the resulting connection data object to that peer.

> Note:
> Each device in the session must perform this step for each nonlocal peer in the session. So if there are four devices in the session, each device must generate a connection data object for each of the other three devices.

1. When your app receives connection data from another peer, it must call [`connectPeer(_:withNearbyConnectionData:)`](/documentation/MultipeerConnectivity/MCSession/connectPeer(_:withNearbyConnectionData:)) to add that peer to the session.

> Note:
> Each of the nonlocal peers must also call ``doc://com.apple.multipeerconnectivity/documentation/MultipeerConnectivity/MCSession/connectPeer(_:withNearbyConnectionData:)`` with the connection data that it received from your app and other nonlocal peers.

You can also cancel an outstanding connection attempt by calling [`cancelConnectPeer(_:)`](/documentation/MultipeerConnectivity/MCSession/cancelConnectPeer(_:)). These methods are described in the Managing Peers Manually group.

### Disconnecting

To leave a session, your app must call [`disconnect()`](/documentation/MultipeerConnectivity/MCSession/disconnect()). For more details, see Leaving a Session.

## Topics

### Creating a Session

[`-  initWithPeer:`](/documentation/MultipeerConnectivity/MCSession/init(peer:))

Creates a Multipeer Connectivity session.

[`-  initWithPeer:securityIdentity:encryptionPreference:`](/documentation/MultipeerConnectivity/MCSession/init(peer:securityIdentity:encryptionPreference:))

Creates a Multipeer Connectivity session, providing security information.

[`delegate`](/documentation/MultipeerConnectivity/MCSession/delegate)

The delegate object that handles session-related events.

[`encryptionPreference`](/documentation/MultipeerConnectivity/MCSession/encryptionPreference)

A value indicating whether the connection prefers encrypted connections, unencrypted connections, or has no preference.

[`myPeerID`](/documentation/MultipeerConnectivity/MCSession/myPeerID)

A local identifier that represents the device on which your app is currently running.

[`securityIdentity`](/documentation/MultipeerConnectivity/MCSession/securityIdentity)

The security identity of the local peer.

### Managing Peers Manually

[`-  connectPeer:withNearbyConnectionData:`](/documentation/MultipeerConnectivity/MCSession/connectPeer(_:withNearbyConnectionData:))

Call this method to connect a peer to the session when using your own service discovery code instead of an  `MCNearbyServiceBrowser` or `MCBrowserViewController` object.

[`-  cancelConnectPeer:`](/documentation/MultipeerConnectivity/MCSession/cancelConnectPeer(_:))

Cancels an attempt to connect to a peer.

[`connectedPeers`](/documentation/MultipeerConnectivity/MCSession/connectedPeers)

An array of all peers that are currently connected to this session.

[`-  nearbyConnectionDataForPeer:withCompletionHandler:`](/documentation/MultipeerConnectivity/MCSession/nearbyConnectionData(forPeer:withCompletionHandler:))

Obtains connection data for the specified peer.

### Sending Data and Resources

[`-  sendData:toPeers:withMode:error:`](/documentation/MultipeerConnectivity/MCSession/send(_:toPeers:with:))

Sends a message to nearby peers.

[`-  sendResourceAtURL:withName:toPeer:withCompletionHandler:`](/documentation/MultipeerConnectivity/MCSession/sendResource(at:withName:toPeer:withCompletionHandler:))

Sends the contents of a URL to a peer.

[`-  startStreamWithName:toPeer:error:`](/documentation/MultipeerConnectivity/MCSession/startStream(withName:toPeer:))

Opens a byte stream to a nearby peer.

### Leaving a Session

[`-  disconnect`](/documentation/MultipeerConnectivity/MCSession/disconnect())

Disconnects the local peer from the session.

### Constants

[`MCSessionSendDataMode`](/documentation/MultipeerConnectivity/MCSessionSendDataMode)

Indicates whether delivery of data should be guaranteed.

[`MCSessionState`](/documentation/MultipeerConnectivity/MCSessionState)

Indicates the current state of a given peer within a session.

[`MCEncryptionPreference`](/documentation/MultipeerConnectivity/MCEncryptionPreference)

Indicates whether a session should use encryption when communicating with nearby peers.

[`Code`](/documentation/MultipeerConnectivity/MCError/Code)

Error codes found in [`MCErrorDomain`](/documentation/MultipeerConnectivity/MCErrorDomain) error domain `NSError` objects returned by methods in the Multipeer Connectivity framework.

[Multipeer Connectivity Error Domain](/documentation/MultipeerConnectivity/multipeer_connectivity_error_domain)

The error domain for errors specific to Multipeer Connectivity.

[Minimum and Maximum Supported Peers](/documentation/MultipeerConnectivity/minimum_and_maximum_supported_peers)

Constants that define the minimum and maximum number of peers supported in a session.

## Relationships

### Conforms To

[`Hashable`](/documentation/Swift/Hashable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)