iOS Developer Library

Developer

GameKit Framework Reference GKSessionDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

GKSessionDelegate

Inheritance


Not Applicable

Conforms To


Import Statement


Swift

import GameKit

Objective-C

@import GameKit;

Availability


Available in iOS 3.0 and later.

An object implements the GKSessionDelegate protocol to control the behavior of a GKSession object. The delegate is called when other visible peers change their state relative to the session. It is also called to determine whether another peer is allowed to connect to the session.

  • Received by the delegate when a peer changes state. (required)

    Declaration

    Swift

    optional func session(_ session: GKSession!, peer peerID: String!, didChangeState state: GKPeerConnectionState)

    Objective-C

    - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state

    Parameters

    session

    The session that received the update.

    peerID

    A string that identifies the peer.

    state

    The state the peer changed to.

    Discussion

    A session calls this method whenever a visible peer changes it state relative to itself. The action your delegate should take depends on what state the peer moved to.

    • When a peer first becomes visible to the session, it appears with a state of GKPeerStateAvailable. Your application should show this peer in its user interface. If the peer changes its state to GKPeerStateUnavailable, it no longer accepts connection requests and your application should remove it from the user interface.

    • The delegate should ignore GKPeerStateConnecting changes and implement the session:didReceiveConnectionRequestFromPeer: method instead.

    • When a peer is connected (GKPeerStateConnected), your application may send data to the peer and receive data from the peer. If a connection to a peer is lost or if the peer deliberately disconnects (GKPeerStateDisconnected), your application should stop sending messages to this peer.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 3.0 and later.

  • Received by the delegate when a remote peer wants to create a connection to the session. (required)

    Declaration

    Swift

    optional func session(_ session: GKSession!, didReceiveConnectionRequestFromPeer peerID: String!)

    Objective-C

    - (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID

    Parameters

    session

    The session that received the request.

    peerID

    A string that uniquely identifies the peer.

    Discussion

    The delegate should call the session’s acceptConnectionFromPeer:error: method if it wants to accept the connection or the denyConnectionFromPeer: method if it wants to refuse the connection.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 3.0 and later.

  • Received by the delegate when an attempt to connect to another peer failed. (required)

    Declaration

    Swift

    optional func session(_ session: GKSession!, connectionWithPeerFailed peerID: String!, withError error: NSError!)

    Objective-C

    - (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error

    Parameters

    session

    The session that received the message.

    peerID

    A string that uniquely identifies the peer.

    error

    The error that occurred.

    Discussion

    The error parameter can be used to inform the user of why the connection failed.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 3.0 and later.

  • Sent to the delegate when a serious error has occurred in the session. (required)

    Declaration

    Swift

    optional func session(_ session: GKSession!, didFailWithError error: NSError!)

    Objective-C

    - (void)session:(GKSession *)session didFailWithError:(NSError *)error

    Parameters

    session

    The session that failed.

    error

    The error that occurred.

    Discussion

    This method is called when a serious internal error occurred in the session. Your application should disconnect the session from other peers and release the session.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 3.0 and later.