iOS Guides | Samples

MultipeerConnectivity.MCSession Class

Represents a persistent connection between multiple devices.

See Also: MCSession

Syntax

[Foundation.Register("MCSession", true)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.TvOS, 10, 0, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 7, 0, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.MacOSX, 10, 10, ObjCRuntime.PlatformArchitecture.Arch64, null)]
public class MCSession : NSObject

Remarks

Multipeer Connectivity has two phases: discovery and the session. The role of the discovery phase is to associate an MCSession object on each device with an MCSession object on the peer devices. In the session phase, the MCSession object is the channel through which devices communicate and its lifecycle events are associated with connections, disconnections, transmissions, and receptions.

The MCSession is instantiated by the application developer. During the discovery phase, there are two roles: advertisers that broadcast their willingness to connect to a certain protocol and browsers that discover these advertisers and invite them to sessions.

Advertising is managed by either the stock MCAdvertiserAssistant or custom controller that uses a MCNearbyServiceAdvertiser. Similarly, browsing is managed by a UIViewController, either the stock MCBrowserViewController or a custom controller that uses a MCNearbyServiceBrowser object to programmatically discover peers. Once a peer is discovered, an invitation is sent with MCNearbyServiceBrowser.InvitePeer). The application user interacts with a system dialog informing them of the invitation. If they accept, the MCSession connects.

The following image shows the sequence of functions with programmatic advertising and browsing:

AdvertiserBrowserDiscussion
Create a new MCNearbyServiceAdvertiserDelegate and assign it to the MCNearbyServiceAdvertiser.Delegate property.Create a new MCNearbyServiceBrowserDelegate and assign it to MCNearbyServiceBrowser.Delegate property.Discovery and connection is taken care of by the delegate objects for the MCNearbyServiceAdvertiser and MCNearbyServiceBrowser.
The advertiser may instantiate a new MCSession now or wait until it receives an invitation.Instantiate a new MCSession.The browser should maintain a reference to a single MCSession object no matter how many peers ultimately connect.
MCNearbyServiceAdvertiser.StartAdvertisingPeerMCNearbyServiceBrowser.StartBrowsingForPeersAdvertiser and browser must use identical serviceType strings to identify their protocol / application. Peer IDs should be unique to each device.
The system will call MCNearbyServiceBrowserDelegate.FoundPeer, passing in a reference to the MCNearbyServiceBrowser. The application developer calls MCNearbyServiceBrowser.InvitePeer, passing in a reference to the previously-created MCSession.The callback is likely to occur on a background thread. If the application developer wishes to update the display, they must use NSObject.InvokeOnMainThread.
The system will call MCNearbyServiceAdvertiserDelegate.DidReceiveInvitationFromPeer. To connect the devices, the application developer must invoke the passed-in invitationHandler with it's context argument set to true and it's session argument set to an MCSession.The callback is likely to occur on a background thread. If the application developer wishes to update the display, they must use NSObject.InvokeOnMainThread.
Once the devices are connected, the MCSession objects can be used to transmit messages and data between devices.

Related content

Requirements

Namespace: MultipeerConnectivity
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0

The members of MultipeerConnectivity.MCSession are listed below.

See Also: NSObject

Public Constructors

Constructs a session with the specified identity for the local peer.
Constructor that allows the application developer to specify security constraints.
Constructor that allows the application developer to specify security constraints and certificates.

Protected Constructors

Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Properties

[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
ConnectedPeersMCPeerID[]. An array of the currently connected devices.
DelegateIMCSessionDelegate. An instance of the MultipeerConnectivity.IMCSessionDelegate model class which acts as the class delegate.
[read-only]
EncryptionPreferenceMCEncryptionPreference. What type, if any, encryption s preferred.
[read-only]
static
MaximumNumberOfPeersnint. Represents the value associated with the constant kMCSessionMaximumNumberOfPeers
[read-only]
static
MinimumNumberOfPeersnint. Represents the value associated with the constant kMCSessionMinimumNumberOfPeers
[read-only]
MyPeerIDMCPeerID. The peer ID associated with this device.
[read-only]
SecurityIdentityNSArray. The security identity of this peer.
WeakDelegateNSObject. An object that can respond to the delegate protocol for this type

Public Methods

CancelConnectPeer(MCPeerID)
Cancel's a pending connection to the peerID.
ConnectPeer(MCPeerID, NSData)
Initiates a connection to a peer identified by peerID.
Disconnect()
Disconnects this peer from the session.
NearbyConnectionDataForPeer(MCPeerID, MCSessionNearbyConnectionDataForPeerCompletionHandler)
Creates the necessary data for a manually-managed peer connection.
NearbyConnectionDataForPeerAsync(MCPeerID) : System.Threading.Tasks.Task<Foundation.NSData>
Creates the necessary data for a manually-managed peer connection.
SendData(NSData, MCPeerID[], MCSessionSendDataMode, out NSError) : Boolean
Enqueues for delivery the data to the peers in peerIDs.
SendResource(NSUrl, String, MCPeerID, Action<Foundation.NSError>) : NSProgress
Enqueues for delivery to peerID the resource at resourceUrl.
SendResourceAsync(NSUrl, String, MCPeerID) : Task
Enqueues for delivery to peerID the resource at resourceUrl.
SendResourceAsync(NSUrl, String, MCPeerID, out NSProgress) : Task
Asynchronously enqueues for delivery to resourceName the resource at resourceUrl, returning a task that represents the operation.
StartStream(String, MCPeerID, out NSError) : NSOutputStream
Creates a named stream to peerID.

Protected Methods

override
Dispose(Boolean)
Releases the resources used by the MCSession object.