Editar

Compartir a través de


MCSession.NearbyConnectionDataForPeerAsync(MCPeerID) Method

Definition

Creates the necessary data for a manually-managed peer connection.

public virtual System.Threading.Tasks.Task<Foundation.NSData> NearbyConnectionDataForPeerAsync (MultipeerConnectivity.MCPeerID peerID);
abstract member NearbyConnectionDataForPeerAsync : MultipeerConnectivity.MCPeerID -> System.Threading.Tasks.Task<Foundation.NSData>
override this.NearbyConnectionDataForPeerAsync : MultipeerConnectivity.MCPeerID -> System.Threading.Tasks.Task<Foundation.NSData>

Parameters

peerID
MCPeerID

Created from data serialized on a remote peer.

Returns

A task that represents the asynchronous NearbyConnectionDataForPeer operation. The value of the TResult parameter is a MCSessionNearbyConnectionDataForPeerCompletionHandler.

Remarks

The NearbyConnectionDataForPeerAsync method is suitable to be used with C# async by returning control to the caller with a Task representing the operation.

Application developers may use a non-Multipeer Connectivity discovery technique, such as Bonjour / NSNetService, and manually manage peer connection. However, the peerID used here and in ConnectPeer(MCPeerID, NSData) must originate from a NSKeyedArchiver serializing an MCPeerID on the remote peer. (This raises the question: if discovery and enough message-passing code to transmit the peerID is done by Bonjour, what's the advantage of using MPC for further communication? One answer might be the evolution of a legacy system, another answer might lie in the simpler message- and resource-passing of MPC.)

Once the application developer has the peerID, the rest of the code to connect a peer would be:

//User code: Perhaps using Bonjour or other discovery and messaging service
var peerID = DeserializedPeerID();
//Request connection data, with completionHandler lambda as continuation
session.NearbyConnectionDataForPeer(peerID, (connectionData, error) => { 
    if(error != null){
        //Note: peerID is serialized version, connectionData is passed in to continuation
        session.ConnectPeer(peerID, connectionData);
    }else{
         throw new Exception(error);
    }
});              

Applies to