MCSession
An MCSession object enables and manages communication among all peers in a Multipeer Connectivity session.
Declaration
class MCSessionInitiating 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
NSDataobject 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
NSURLobject to the specified peer. The URL can be either a local file URL or a web URL. ThecompletionHandlerblock is called when the resource is fully received by the recipient peer or when an error occurs during transmission.This method returns an
NSProgressobject 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
NSInputStreamobject 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.
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.
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.
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
init(peer:)init(peer:securityIdentity:encryptionPreference:)delegateencryptionPreferencemyPeerIDsecurityIdentity
Managing Peers Manually
connectPeer(_:withNearbyConnectionData:)cancelConnectPeer(_:)connectedPeersnearbyConnectionData(forPeer:withCompletionHandler:)
Sending Data and Resources
send(_:toPeers:with:)sendResource(at:withName:toPeer:withCompletionHandler:)startStream(withName:toPeer:)