Bewerken

Delen via


MultipeerConnectivity Namespace

The MultipeerConnectivity namespace provides local (WiFi/Bluetooth) peer-to-peer messaging and data connections.

Classes

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 MCAdvertiserAssistant.

MCAdvertiserAssistantDelegate_Extensions

Extension methods to the IMCAdvertiserAssistantDelegate interface to support all the methods from the MCAdvertiserAssistantDelegate protocol.

MCBrowserViewController

A standard T: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 IMCBrowserViewControllerDelegate interface to support all the methods from the MCBrowserViewControllerDelegate protocol.

MCErrorExtensions

Extension methods for the MultipeerConnectivity.MCError enumeration.

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 MCNearbyServiceAdvertiser object.

MCNearbyServiceAdvertiserDelegate_Extensions

Extension methods to the IMCNearbyServiceAdvertiserDelegate interface to support all the methods from the MCNearbyServiceAdvertiserDelegate protocol.

MCNearbyServiceBrowser

Allows programmatic browsing for devices advertising for multipeer connetivity.

MCNearbyServiceBrowserDelegate

A delegate object that exposes peer-discovery events for a MCNearbyServiceBrowser object.

MCNearbyServiceBrowserDelegate_Extensions

Extension methods to the IMCNearbyServiceBrowserDelegate interface to support all the methods from the 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 MCSession life-cycle, such as connection status changes and data reception.

MCSessionDelegate_Extensions

Extension methods to the IMCSessionDelegate interface to support all the methods from the MCSessionDelegate protocol.

Interfaces

IMCAdvertiserAssistantDelegate

Interface representing the required methods (if any) of the protocol MCAdvertiserAssistantDelegate.

IMCBrowserViewControllerDelegate

Interface representing the required methods (if any) of the protocol MCBrowserViewControllerDelegate.

IMCNearbyServiceAdvertiserDelegate

Interface representing the required methods (if any) of the protocol MCNearbyServiceAdvertiserDelegate.

IMCNearbyServiceBrowserDelegate

Interface representing the required methods (if any) of the protocol MCNearbyServiceBrowserDelegate.

IMCSessionDelegate

Interface representing the required methods (if any) of the protocol MCSessionDelegate.

Enums

MCEncryptionPreference

An enumeration whose values specify whether an MCSession should encrypt its connection. Used with C:MultipeerConnectivity.MCSession(MultipeerConnectivity.MCPeerID,Security.SecIdentity, MultipeerConnectivity.MCEncryptionPreference).

MCError

An enumeration whose values specify various errors relating to multipeer connectivity.

MCSessionSendDataMode

An enumeration whose values specify whether a message's delivery is guaranteed. Used with SendData(NSData, MCPeerID[], MCSessionSendDataMode, NSError).

MCSessionState

An enumeration whose values indicate the state of a MCSession. Used with DidChangeState(MCSession, MCPeerID, MCSessionState).

Delegates

MCNearbyServiceAdvertiserInvitationHandler

The delegate that serves as the invitation handler in calls to DidReceiveInvitationFromPeer(MCNearbyServiceAdvertiser, MCPeerID, NSData, MCNearbyServiceAdvertiserInvitationHandler).

MCSessionNearbyConnectionDataForPeerCompletionHandler

A delegate that serves as the completion handler for NearbyConnectionDataForPeer(MCPeerID, MCSessionNearbyConnectionDataForPeerCompletionHandler).

Remarks

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 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 T:MultipeerConnectivity.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 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 Delegate property.Create a new MCNearbyServiceBrowserDelegate and assign it to 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.

M:MultipeerConnectivity.MCNearbyServiceAdvertiser.StartAdvertisingPeer*

M: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 FoundPeer, passing in a reference to the MCNearbyServiceBrowser. The application developer calls 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 M:Foundation.NSObject.InvokeOnMainThread*.
The system will call 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 M:Foundation.NSObject.InvokeOnMainThread*.
Once the devices are connected, the MCSession objects can be used to transmit messages and data between devices.

See also