GKSessionDelegate Protocol Reference

Conforms to
Framework
/System/Library/Frameworks/GameKit.framework
Availability
Available in iOS 3.0 and later.
Companion guide
Declared in
GKPublicProtocols.h

Overview

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.

Tasks

Observing Changes to Peers

Connection Requests from Other Peers

Connection Errors

Instance Methods

session:connectionWithPeerFailed:withError:

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

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

Availability
  • Available in iOS 3.0 and later.
Declared In
GKPublicProtocols.h

session:didFailWithError:

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

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

Availability
  • Available in iOS 3.0 and later.
Declared In
GKPublicProtocols.h

session:didReceiveConnectionRequestFromPeer:

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

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

Availability
  • Available in iOS 3.0 and later.
Declared In
GKPublicProtocols.h

session:peer:didChangeState:

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

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

Availability
  • Available in iOS 3.0 and later.
Declared In
GKPublicProtocols.h