Class

MCSession

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

Declaration

@interface MCSession : NSObject

Overview

Initiating a Session

To set up a session:

  1. Use the initWithDisplayName: 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).

  2. Use the peer ID with the method initWithPeer: to initialize the session object.

  3. 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.)

  4. 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:didChangeState: method with MCSessionStateConnected 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:

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.

  2. Exchange connection data. After you have obtained the nearby peer’s ID object, call nearbyConnectionDataForPeer: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.

  3. When your app receives connection data from another peer, it must call connectPeer:withNearbyConnectionData: to add that peer to the session.

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

- initWithPeer:

Creates a Multipeer Connectivity session.

- initWithPeer:securityIdentity:encryptionPreference:

Creates a Multipeer Connectivity session, providing security information.

delegate

The delegate object that handles session-related events.

encryptionPreference

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

myPeerID

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

securityIdentity

The security identity of the local peer.

Managing Peers Manually

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

Cancels an attempt to connect to a peer.

connectedPeers

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

- nearbyConnectionDataForPeer:withCompletionHandler:

Obtains connection data for the specified peer.

Sending Data and Resources

- sendData:toPeers:withMode:error:

Sends a message encapsulated in an NSData object to nearby peers.

- startStreamWithName:toPeer:error:

Opens a byte stream to a nearby peer.

Leaving a Session

- disconnect

Disconnects the local peer from the session.

Constants

MCSessionSendDataMode

Indicates whether delivery of data should be guaranteed.

MCSessionState

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

MCEncryptionPreference

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

MCErrorCode

Error codes found in MCErrorDomain error domain NSError objects returned by methods in the Multipeer Connectivity framework.

Multipeer Connectivity Error Domain

The error domain for errors specific to Multipeer Connectivity.

Minimum and Maximum Supported Peers

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

Relationships

Inherits From