Class: GKSession
Overview
A GKSession object provides the ability to discover and connect to nearby iOS devices using Bluetooth or Wi-fi.
Instance Attribute Summary (collapse)
-
- available
A Boolean value that determines whether or not the session wants to connect to new peers.
-
- delegate
The delegate of the session object.
-
- disconnectTimeout
A time interval that expresses how long the session waits before it disconnects a nonresponsive peer.
-
- displayName
readonly
The name of the user.
-
- peerID
readonly
A string that identifies your session to other peers.
-
- sessionID
readonly
A string used to filter the list of peers who are allowed to see your session.
-
- sessionMode
readonly
The mode the session uses to find other peers.
Instance Method Summary (collapse)
-
- acceptConnectionFromPeer:error:
Called by the delegate to accept a connection request received from a remote peer.
-
- cancelConnectToPeer:
Cancels a pending request to connect to another iOS device.
-
- connectToPeer:withTimeout:
Creates a connection to another iOS device.
-
- denyConnectionFromPeer:
Called by the delegate to reject a connection request received from a remote peer.
-
- disconnectFromAllPeers
Disconnects the session from all connected peers.
-
- disconnectPeerFromAllPeers:
Disconnects a connected peer from all peers connected to the session.
-
- displayNameForPeer:
Returns a user-readable name for a peer.
-
- initWithSessionID:displayName:sessionMode:
Initializes and returns a newly allocated session.
-
- peersWithConnectionState:
Returns a list of peers in the specified connection state.
-
- sendData:toPeers:withDataMode:error:
Transmits a collection of bytes to a list of connected peers.
-
- sendDataToAllPeers:withDataMode:error:
Transmits a collection of bytes to all connected peers.
-
- setDataReceiveHandler:withContext:
Sets the object that handles data received from other peers connected to the session.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Instance Attribute Details
- (Boolean) available
A Boolean value that determines whether or not the session wants to connect to new peers. When available is YES, the session is visible to other peers based on its sessionMode property. When available is set to NO, it remains connected to peers, but is no longer visible to nonconnected peers. The default is NO.Typically, your application configures the session object with a delegate and data receiver, and then sets available to YES. When the delegate finishes connecting to peers, it should set the session’s available property to NO.
- (Object) delegate
The delegate of the session object. A session’s delegate is responsible for observing changes to other peers running with the same session ID. Your application must set a delegate before making your session known to other peers.
- (NSTimeInterval) disconnectTimeout
A time interval that expresses how long the session waits before it disconnects a nonresponsive peer. The timeout is the waiting period before disconnecting a peer from the session. If a peer is disconnected, the delegate’s session:peer:didChangeState: method is called.
- (String) displayName (readonly)
The name of the user. (read-only) The display name is transmitted to visible peers so that they can present a human-readable name for your session.
- (String) peerID (readonly)
A string that identifies your session to other peers. (read-only)
- (String) sessionID (readonly)
A string used to filter the list of peers who are allowed to see your session. (read-only) The session ID is used by sessions configured as servers to advertise itself to other peers and by sessions configured as clients to search for compatible servers. The session ID should be the short name of an approved Bonjour service type.
- (GKSessionMode) sessionMode (readonly)
The mode the session uses to find other peers. (read-only) The session mode changes the behavior of the session when available is set to YES.A GKSessionModeServer session advertises itself to local devices using its session ID.A GKSessionModeClient session searches for local devices advertising the same session ID. As it discovers available and compatible peers, it calls the delegate’s session:peer:didChangeState: method.A GKSessionModePeer session both advertises as a server and searches as a client.
Instance Method Details
- (Boolean) acceptConnectionFromPeer(peerID, error:error)
Called by the delegate to accept a connection request received from a remote peer. When your session acts as a server, client peers can discover your session and attempt to connect to it. When a client attempts to connect to the session, the delegate’s session:didReceiveConnectionRequestFromPeer: method is called to decide whether the peer should be connected. Your application calls this method to accept the request, or denyConnectionFromPeer: to reject it.
- (Object) cancelConnectToPeer(peerID)
Cancels a pending request to connect to another iOS device. Your application previously called connectToPeer:withTimeout: to create a connection to another iOS device. When your application cancels the connection attempt, both delegates’ session:connectionWithPeerFailed:withError: methods are called.If your application already connected to the peer, your application should call disconnectFromAllPeers instead.
- (Object) connectToPeer(peerID, withTimeout:timeout)
Creates a connection to another iOS device. When your application is acting as a client, it calls this method to connect to an available peer it discovered. When your application calls this method, a request is transmitted to the remote peer, who chooses whether to accept or reject the connection request.If the connection to the remote peer is successful, the delegate’s session:peer:didChangeState: method is called for each peer it successfully connected to. If the connection fails or your application cancels the connection attempt, the session calls the delegate’s session:connectionWithPeerFailed:withError: method.
- (Object) denyConnectionFromPeer(peerID)
Called by the delegate to reject a connection request received from a remote peer. When your session acts as a server, client peers can discover your session and attempt to connect to it. When a client attempts to connect to the session, the delegate’s session:didReceiveConnectionRequestFromPeer: method is called to decide whether the peer should be connected. Your application calls this method to reject the request or acceptConnectionFromPeer:error: to accept it.
- (Object) disconnectFromAllPeers
Disconnects the session from all connected peers.
- (Object) disconnectPeerFromAllPeers(peerID)
Disconnects a connected peer from all peers connected to the session.
- (String) displayNameForPeer(peerID)
Returns a user-readable name for a peer. The display name is used to populate your user interface with the names of other peers visible to the session.
- (Object) initWithSessionID(sessionID, displayName:name, sessionMode:mode)
Initializes and returns a newly allocated session. Only sessions running with the same sessionID are visible to your session.
- (Array) peersWithConnectionState(state)
Returns a list of peers in the specified connection state.
- (Boolean) sendData(data, toPeers:peers, withDataMode:mode, error:error)
Transmits a collection of bytes to a list of connected peers. The session queues the data and transmits it in the order it was queued. Data transmitted unreliably may be received out of order by the other peers.
- (Boolean) sendDataToAllPeers(data, withDataMode:mode, error:error)
Transmits a collection of bytes to all connected peers. The session queues the data and transmits it when the network is free.
- (Object) setDataReceiveHandler(handler, withContext:context)
Sets the object that handles data received from other peers connected to the session. The handler must implement a method with the following signature:where data contains the bytes received from a remote peer, peer is a string that identifies the peer, session is the session that received the data, and context is the same context that was passed into the original call to setDataReceiveHandler:withContext:.Important: Data received from other peers should be treated as untrusted data. Be sure to validate the data you receive from the session and write your code carefully to avoid security vulnerabilities. See the Secure Coding Guide for more information.