Introduced in iOS 7, Multipeer Connectivity (MPC) allows iOS devices to discover and communicate with other nearby iOS devices. "Nearby" devices are those within Bluetooth range, on the same WiFi network segment, or using WiFi peer connectivity. Additionally, MPC can bridge through connected peers: a WiFi-only iOS device can bridge to a Bluetooth-only iOS device via a peer that has both WiFi and Bluetooth . MPC is considerably easier to use than Zero-Configuration Networking (zeroconf), but MPC is not available for cross-platform scenarios.
Multipeer Connectivity has two phases: discovery and the session. The role of the discovery phase is to associate an MultipeerConnectivity.MCSession object on each device with an MultipeerConnectivity.MCSession object on the peer devices. In the session phase, the MultipeerConnectivity.MCSession object is the channel through which devices communicate and its lifecycle events are associated with connections, disconnections, transmissions, and receptions.
The MultipeerConnectivity.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 MultipeerConnectivity.MCAdvertiserAssistant or custom controller that uses a MultipeerConnectivity.MCNearbyServiceAdvertiser. Similarly, browsing is managed by a MultipeerConnectivity.UIViewController, either the stock MultipeerConnectivity.MCBrowserViewController or a custom controller that uses a MultipeerConnectivity.MCNearbyServiceBrowser object to programmatically discover peers. Once a peer is discovered, an invitation is sent with MultipeerConnectivity.MCNearbyServiceBrowser.InvitePeer). The application user interacts with a system dialog informing them of the invitation. If they accept, the MultipeerConnectivity.MCSession connects.
The following image shows the sequence of functions with programmatic advertising and browsing:
Advertiser | Browser | Discussion |
---|---|---|
Create a new MultipeerConnectivity.MCNearbyServiceAdvertiserDelegate and assign it to the MultipeerConnectivity.MCNearbyServiceAdvertiser.Delegate property. | Create a new MultipeerConnectivity.MCNearbyServiceBrowserDelegate and assign it to MultipeerConnectivity.MCNearbyServiceBrowser.Delegate property. | Discovery and connection is taken care of by the delegate objects for the MultipeerConnectivity.MCNearbyServiceAdvertiser and MultipeerConnectivity.MCNearbyServiceBrowser. |
The advertiser may instantiate a new MultipeerConnectivity.MCSession now or wait until it receives an invitation. | Instantiate a new MultipeerConnectivity.MCSession. | The browser should maintain a reference to a single MultipeerConnectivity.MCSession object no matter how many peers ultimately connect. |
MultipeerConnectivity.MCNearbyServiceAdvertiser.StartAdvertisingPeer | MultipeerConnectivity.MCNearbyServiceBrowser.StartBrowsingForPeers | Advertiser and browser must use identical serviceType strings to identify their protocol / application. Peer IDs should be unique to each device. |
The system will call MultipeerConnectivity.MCNearbyServiceBrowserDelegate.FoundPeer, passing in a reference to the MultipeerConnectivity.MCNearbyServiceBrowser. The application developer calls MultipeerConnectivity.MCNearbyServiceBrowser.InvitePeer, passing in a reference to the previously-created MultipeerConnectivity.MCSession. | The callback is likely to occur on a background thread. If the application developer wishes to update the display, they must use Foundation.NSObject.InvokeOnMainThread. | |
The system will call MultipeerConnectivity.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 MultipeerConnectivity.MCSession. | The callback is likely to occur on a background thread. If the application developer wishes to update the display, they must use Foundation.NSObject.InvokeOnMainThread. | |
Once the devices are connected, the MultipeerConnectivity.MCSession objects can be used to transmit messages and data between devices. |
Type | Reason |
---|---|
IMCAdvertiserAssistantDelegate | Interface representing the required methods (if any) of the protocol MultipeerConnectivity.MCAdvertiserAssistantDelegate. |
IMCBrowserViewControllerDelegate | Interface representing the required methods (if any) of the protocol MultipeerConnectivity.MCBrowserViewControllerDelegate. |
IMCNearbyServiceAdvertiserDelegate | Interface representing the required methods (if any) of the protocol MultipeerConnectivity.MCNearbyServiceAdvertiserDelegate. |
IMCNearbyServiceBrowserDelegate | Interface representing the required methods (if any) of the protocol MultipeerConnectivity.MCNearbyServiceBrowserDelegate. |
IMCSessionDelegate | Interface representing the required methods (if any) of the protocol MultipeerConnectivity.MCSessionDelegate. |
MCAdvertiserAssistant | A convenience class that manages the process of advertising for multipeer connectivity and interacting with the application user. |
MCAdvertiserAssistantDelegate | A delegate object that provides events for the presentation or dismissal of an invitation by a MultipeerConnectivity.MCAdvertiserAssistant. |
MCAdvertiserAssistantDelegate_Extensions | Extension methods to the MultipeerConnectivity.IMCAdvertiserAssistantDelegate interface to support all the methods from the MultipeerConnectivity.MCAdvertiserAssistantDelegate protocol. |
MCBrowserViewController | A standard MultipeerConnectivity.UIViewController that provides a user experience for browsing for peers. |
MCBrowserViewControllerDelegate | A delegate object that provides events relating to the presentation of discovered peers and the application user's selection or cancellation of them. |
MCBrowserViewControllerDelegate_Extensions | Extension methods to the MultipeerConnectivity.IMCBrowserViewControllerDelegate interface to support all the methods from the MultipeerConnectivity.MCBrowserViewControllerDelegate protocol. |
MCEncryptionPreference | An enumeration whose values specify whether an MultipeerConnectivity.MCSession should encrypt its connection. Used with MultipeerConnectivity.MCSession(MultipeerConnectivity.MCPeerID, Security.SecIdentity, MultipeerConnectivity.MCEncryptionPreference). |
MCError | An enumeration whose values specify various errors relating to multipeer connectivity. |
MCNearbyServiceAdvertiser | Provides programmatic control for advertising the device for multipeer connectivity. |
MCNearbyServiceAdvertiserDelegate | A delegate object that exposes events relating to advertising and invitations for multipeer connectivity for a MultipeerConnectivity.MCNearbyServiceAdvertiser object. |
MCNearbyServiceAdvertiserDelegate_Extensions | Extension methods to the MultipeerConnectivity.IMCNearbyServiceAdvertiserDelegate interface to support all the methods from the MultipeerConnectivity.MCNearbyServiceAdvertiserDelegate protocol. |
MCNearbyServiceAdvertiserInvitationHandler | The delegate that serves as the invitation handler in calls to MultipeerConnectivity.MCNearbyServiceAdvertiserDelegate.DidReceiveInvitationFromPeer. |
MCNearbyServiceBrowser | Allows programmatic browsing for devices advertising for multipeer connetivity. |
MCNearbyServiceBrowserDelegate | A delegate object that exposes peer-discovery events for a MultipeerConnectivity.MCNearbyServiceBrowser object. |
MCNearbyServiceBrowserDelegate_Extensions | Extension methods to the MultipeerConnectivity.IMCNearbyServiceBrowserDelegate interface to support all the methods from the MultipeerConnectivity.MCNearbyServiceBrowserDelegate protocol. |
MCPeerID | Identifies a device in a multipeer connectivity network. |
MCSession | Represents a persistent connection between multiple devices. |
MCSessionDelegate | A delegate object whose functions relate to events in the MultipeerConnectivity.MCSession life-cycle, such as connection status changes and data reception. |
MCSessionDelegate_Extensions | Extension methods to the MultipeerConnectivity.IMCSessionDelegate interface to support all the methods from the MultipeerConnectivity.MCSessionDelegate protocol. |
MCSessionNearbyConnectionDataForPeerCompletionHandler | A delegate that serves as the completion handler for MultipeerConnectivity.MCSession.NearbyConnectionDataForPeer. |
MCSessionSendDataMode | An enumeration whose values specify whether a message's delivery is guaranteed. Used with MultipeerConnectivity.MCSession.SendData. |
MCSessionState | An enumeration whose values indicate the state of a MultipeerConnectivity.MCSession. Used with MultipeerConnectivity.MCSessionDelegate.DidChangeState. |