GKSession object provides the ability to discover and connect to nearby iOS devices using Bluetooth or Wi-fi.
- iOS 3.0–7.0Deprecated
- macOS 10.8–10.10Deprecated
- watchOS 2.0–2.0Deprecated
Sessions primarily work with peers. A peer is any iOS device made visible by creating and configuring a
GKSession object. Each peer is identified by a unique identifier, called a peer id (
peer) string. Your application can use a
peer 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 (
session). 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
GKSession 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 methods are thread-safe and may be called from any thread. However, the session always calls its delegate on the main thread.