---
title: MCSession
framework: multipeerconnectivity
role: symbol
role_heading: Class
path: multipeerconnectivity/mcsession
---

# MCSession

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

## Declaration

```swift
class MCSession
```

## Initiating a Session

Initiating a Session To set up a session: Use the init(displayName:) method of the 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). Use the peer ID with the method init(peer:) to initialize the session object. Invite peers to join the session using an MCNearbyServiceBrowser object, an MCBrowserViewController object, or your own peer discovery code. (Sessions currently support up to 8 peers, including the local peer.) Set up an MCNearbyServiceAdvertiser object or 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:) method with 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 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:) sends an NSData object to the specified peers. On each recipient device, the delegate object’s session(_:didReceive:fromPeer:) method is called with the data object when the data has been fully received. 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:) method when the device begins receiving the resource, and calls its session(_:didFinishReceivingResourceWithName:fromPeer:at:withError:) method when the resource has been fully received or when an error occurs. 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:) 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: 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 init(displayName:). A peer ID object must be constructed on the device that it represents. Exchange connection data. After you have obtained the nearby peer’s ID object, call 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. When your app receives connection data from another peer, it must call connectPeer(_:withNearbyConnectionData:) to add that peer to the session. note: Each of the nonlocal peers must also call 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(_:). These methods are described in the Managing Peers Manually group. Disconnecting To leave a session, your app must call disconnect(). For more details, see Leaving a Session.

## Topics

### Creating a Session

- [init(peer:)](multipeerconnectivity/mcsession/init(peer:).md)
- [init(peer:securityIdentity:encryptionPreference:)](multipeerconnectivity/mcsession/init(peer:securityidentity:encryptionpreference:).md)
- [delegate](multipeerconnectivity/mcsession/delegate.md)
- [encryptionPreference](multipeerconnectivity/mcsession/encryptionpreference.md)
- [myPeerID](multipeerconnectivity/mcsession/mypeerid.md)
- [securityIdentity](multipeerconnectivity/mcsession/securityidentity.md)

### Managing Peers Manually

- [connectPeer(_:withNearbyConnectionData:)](multipeerconnectivity/mcsession/connectpeer(_:withnearbyconnectiondata:).md)
- [cancelConnectPeer(_:)](multipeerconnectivity/mcsession/cancelconnectpeer(_:).md)
- [connectedPeers](multipeerconnectivity/mcsession/connectedpeers.md)
- [nearbyConnectionData(forPeer:withCompletionHandler:)](multipeerconnectivity/mcsession/nearbyconnectiondata(forpeer:withcompletionhandler:).md)

### Sending Data and Resources

- [send(_:toPeers:with:)](multipeerconnectivity/mcsession/send(_:topeers:with:).md)
- [sendResource(at:withName:toPeer:withCompletionHandler:)](multipeerconnectivity/mcsession/sendresource(at:withname:topeer:withcompletionhandler:).md)
- [startStream(withName:toPeer:)](multipeerconnectivity/mcsession/startstream(withname:topeer:).md)

### Leaving a Session

- [disconnect()](multipeerconnectivity/mcsession/disconnect().md)

### Constants

- [MCSessionSendDataMode](multipeerconnectivity/mcsessionsenddatamode.md)
- [MCSessionState](multipeerconnectivity/mcsessionstate.md)
- [MCEncryptionPreference](multipeerconnectivity/mcencryptionpreference.md)
- [MCError.Code](multipeerconnectivity/mcerror/code.md)
- [Multipeer Connectivity Error Domain](multipeerconnectivity/multipeer_connectivity_error_domain.md)
- [Minimum and Maximum Supported Peers](multipeerconnectivity/minimum_and_maximum_supported_peers.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)

## See Also

### Classes

- [MCAdvertiserAssistant](multipeerconnectivity/mcadvertiserassistant.md)
- [MCBrowserViewController](multipeerconnectivity/mcbrowserviewcontroller.md)
- [MCNearbyServiceAdvertiser](multipeerconnectivity/mcnearbyserviceadvertiser.md)
- [MCNearbyServiceBrowser](multipeerconnectivity/mcnearbyservicebrowser.md)
- [MCPeerID](multipeerconnectivity/mcpeerid.md)
