<!--
{
  "availability" : [
    "iOS: 3.0.0 - 7.0.0",
    "iPadOS: 3.0.0 - 7.0.0",
    "macOS: 10.8.0 - 10.10.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "GameKit",
  "identifier" : "/documentation/GameKit/GKSession",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "GameKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKSession"
  },
  "title" : "GKSession"
}
-->

# GKSession

A [`GKSession`](/documentation/GameKit/GKSession) object provides the ability to discover and connect to nearby iOS devices using Bluetooth or Wi-fi.

```
class GKSession
```

## Overview

Sessions primarily work with *peers*. A peer is any iOS device made visible by creating and configuring a [`GKSession`](/documentation/GameKit/GKSession) object. Each peer is identified by a unique identifier, called a peer id ([`peerID`](/documentation/GameKit/GKSession/peerID)) string. Your application can use a [`peerID`](/documentation/GameKit/GKSession/peerID) string to obtain a user-readable name for a remote peer and to attempt to connect to that peer. Similarly, your session’s peer ID is visible to other nearby peers. After a connection is established, your application uses the remote peer’s ID to address data packets that it wants to send.

Peers discover other peers by using a unique string to identify the service they implement, called a session ID ([`sessionID`](/documentation/GameKit/GKSession/sessionID)). Sessions can be configured to broadcast a session ID (as a *server*), to search for other peers advertising with that session ID (as a *client*), or to act as both a server and a client simultaneously (as a *peer*.

Your application controls the behavior of a session through a delegate that implements the [`GKSessionDelegate`](/documentation/GameKit/GKSessionDelegate) protocol. The delegate is called when remote peers are discovered, when those peers attempt to connect to the session, and when the state of a remote peer changes. Your application also provides a data handler to the session so that the session can forward data it receives from remote peers. The data handler can be a separate object or the same object as the delegate.

When Bluetooth is turned on, Wi-Fi download speeds drastically decrease while the device is searching for other Bluetooth enabled devices. After the Bluetooth discovery time has completed, Wi-Fi speeds return to normal.

[`GKSession`](/documentation/GameKit/GKSession) methods are thread-safe and may be called from any thread. However, the session always calls its delegate on the main thread.

## Topics

### Creating a Session

[`-  initWithSessionID:displayName:sessionMode:`](/documentation/GameKit/GKSession/init(sessionID:displayName:sessionMode:))

Initializes and returns a newly allocated session.

### Setting and Getting the Delegate

[`delegate`](/documentation/GameKit/GKSession/delegate)

The delegate of the session object.

### Searching for Other Peers

[`available`](/documentation/GameKit/GKSession/isAvailable)

A Boolean value that determines whether or not the session wants to connect to new peers.

### Obtaining Information About Other Peers

[`-  peersWithConnectionState:`](/documentation/GameKit/GKSession/peers(with:))

Returns a list of peers in the specified connection state.

[`-  displayNameForPeer:`](/documentation/GameKit/GKSession/displayName(forPeer:))

Returns a user-readable name for a peer.

### Connecting to a Remote Peer

[`-  connectToPeer:withTimeout:`](/documentation/GameKit/GKSession/connect(toPeer:withTimeout:))

Creates a connection to another iOS device.

[`-  cancelConnectToPeer:`](/documentation/GameKit/GKSession/cancelConnect(toPeer:))

Cancels a pending request to connect to another iOS device.

### Receiving Connections from a Remote Peer

[`-  acceptConnectionFromPeer:error:`](/documentation/GameKit/GKSession/acceptConnection(fromPeer:))

Called by the delegate to accept a connection request received from a remote peer.

[`-  denyConnectionFromPeer:`](/documentation/GameKit/GKSession/denyConnection(fromPeer:))

Called by the delegate to reject a connection request received from a remote peer.

### Working with Connected Peers

[`-  setDataReceiveHandler:withContext:`](/documentation/GameKit/GKSession/setDataReceiveHandler(_:withContext:))

Sets the object that handles data received from other peers connected to the session.

[`-  sendData:toPeers:withDataMode:error:`](/documentation/GameKit/GKSession/send(_:toPeers:with:))

Transmits a collection of bytes to a list of connected peers.

[`-  sendDataToAllPeers:withDataMode:error:`](/documentation/GameKit/GKSession/sendData(toAllPeers:with:))

Transmits a collection of bytes to all connected peers.

[`disconnectTimeout`](/documentation/GameKit/GKSession/disconnectTimeout)

A time interval that expresses how long the session waits before it disconnects a nonresponsive peer.

[`-  disconnectFromAllPeers`](/documentation/GameKit/GKSession/disconnectFromAllPeers())

Disconnects the session from all connected peers.

[`-  disconnectPeerFromAllPeers:`](/documentation/GameKit/GKSession/disconnectPeer(fromAllPeers:))

Disconnects a connected peer from all peers connected to the session.

### Information about the Session

[`displayName`](/documentation/GameKit/GKSession/displayName)

The name of the user.

[`peerID`](/documentation/GameKit/GKSession/peerID)

A string that identifies your session to other peers.

[`sessionID`](/documentation/GameKit/GKSession/sessionID)

A string used to filter the list of peers who are allowed to see your session.

[`sessionMode`](/documentation/GameKit/GKSession/sessionMode)

The mode the session uses to find other peers.

### Constants

[`GKSendDataMode`](/documentation/GameKit/GKSendDataMode)

The mechanism used to transmit data to other peers.

[`GKSessionMode`](/documentation/GameKit/GKSessionMode)

Modes that determine how a session interacts with other peers.

[`GKPeerConnectionState`](/documentation/GameKit/GKPeerConnectionState)

The state of a peer known to the session.

[The Session Error Domain](/documentation/GameKit/the-session-error-domain)

The [`GKSession`](/documentation/GameKit/GKSession) error domain.

[`Code`](/documentation/GameKit/GKSessionError-swift.struct/Code)

Error codes for the session error domain.

[`GKSessionError`](/documentation/GameKit/GKSessionError-swift.struct)

## Relationships

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)