iOS Developer Library

Developer

MultipeerConnectivity Framework Reference MCSessionDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

MCSessionDelegate

Inheritance


Not Applicable

Import Statement


Swift

import MultipeerConnectivity

Objective-C

@import MultipeerConnectivity;

Availability


Available in iOS 7.0 and later.

The MCSessionDelegate protocol defines methods that a delegate of the MCSession class can implement to handle session-related events. For more information, see MCSession Class Reference.

  • Indicates that an NSData object has been received from a nearby peer. (required)

    Declaration

    Swift

    func session(_ session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!)

    Objective-C

    - (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID

    Parameters

    session

    The session through which the data was received.

    data

    An object containing the received data.

    peerID

    The peer ID of the sender.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Indicates that the local peer began receiving a resource from a nearby peer. (required)

    Declaration

    Swift

    func session(_ session: MCSession!, didStartReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, withProgress progress: NSProgress!)

    Objective-C

    - (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress

    Parameters

    session

    The session that started receiving the resource.

    resourceName

    The name of the resource, as provided by the sender.

    peerID

    The sender’s peer ID.

    progress

    An NSProgress object that can be used to cancel the transfer or queried to determine how far the transfer has progressed.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Indicates that the local peer finished receiving a resource from a nearby peer. (required)

    Declaration

    Swift

    func session(_ session: MCSession!, didFinishReceivingResourceWithName resourceName: String!, fromPeer peerID: MCPeerID!, atURL localURL: NSURL!, withError error: NSError!)

    Objective-C

    - (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error

    Parameters

    session

    The session through which the data was received.

    resourceName

    The name of the resource, as provided by the sender.

    peerID

    The peer ID of the sender.

    localURL

    An NSURL object that provides the location of a temporary file containing the received data.

    error

    An error object indicating what went wrong if the file was not received successfully, or nil.

    Discussion

    The file referenced by resourceURL is a temporary file. Your app must either read the file or make a copy in a permanent location before this delegate method returns.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Called when a nearby peer opens a byte stream connection to the local peer. (required)

    Declaration

    Swift

    func session(_ session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!)

    Objective-C

    - (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID

    Parameters

    session

    The session through which the byte stream was opened.

    stream

    An NSInputStream object that represents the local endpoint for the byte stream.

    streamName

    The name of the stream, as provided by the originator.

    peerID

    The peer ID of the originator of the stream.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Called when the state of a nearby peer changes. (required)

    Declaration

    Swift

    func session(_ session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState)

    Objective-C

    - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state

    Parameters

    session

    The session that manages the nearby peer whose state changed.

    peerID

    The ID of the nearby peer whose state changed.

    state

    The new state of the nearby peer.

    Discussion

    This delegate method is called with the following state values when the nearby peer’s state changes:

    • MCSessionStateConnected—the nearby peer accepted the invitation and is now connected to the session.

    • MCSessionStateNotConnected—the nearby peer declined the invitation, the connection could not be established, or a previously connected peer is no longer connected.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.

  • Called to validate the client certificate provided by a peer when the connection is first established.

    Declaration

    Swift

    optional func session(_ session: MCSession!, didReceiveCertificate certificate: [AnyObject]!, fromPeer peerID: MCPeerID!, certificateHandler certificateHandler: ((Bool) -> Void)!)

    Objective-C

    - (void)session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler

    Parameters

    session

    The session that the nearby peer wishes to join.

    certificate

    A certificate chain, presented as an array of SecCertificateRef certificate objects. The first certificate in this chain is the peer’s certificate, which is derived from the identity that the peer provided when it called the initWithPeer:securityIdentity:encryptionPreference: method. The other certificates are the (optional) additional chain certificates provided in that same array.

    If the nearby peer did not provide a security identity, then this parameter’s value is nil.

    peerID

    The peer ID of the sender.

    certificateHandler

    Your app should call this handler with a value of YEStrue if the nearby peer should be allowed to join the session, or NOfalse otherwise.

    Discussion

    Your app should inspect the nearby peer’s certificate, and then should decide whether to trust that certificate. Upon making that determination, your app should call the provided certificateHandler block, passing either YEStrue (to trust the nearby peer) or NOfalse (to reject it).

    For information about validating certificates, read Cryptographic Services Guide.

    Import Statement

    Objective-C

    @import MultipeerConnectivity;

    Swift

    import MultipeerConnectivity

    Availability

    Available in iOS 7.0 and later.