Class: GKSession

Inherits:
NSObject show all

Overview

A GKSession object provides the ability to discover and connect to nearby iOS devices using Bluetooth or Wi-fi.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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.

Returns:

  • (Boolean)

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

Returns:

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

Returns:

  • (NSTimeInterval)

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

Returns:

- (String) peerID (readonly)

A string that identifies your session to other peers. (read-only)

Returns:

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

Returns:

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

Returns:

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.

Parameters:

  • peerID (String)

    The string identifying the peer that initiated the connection to the session.

  • error (Pointer)

    If an error occurred when connecting the peer, upon return contains an NSError object that explains the problem.

Returns:

  • (Boolean)

    YES if a connection was established to the remote peer; NO if an error occurred.

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

Parameters:

  • peerID (String)

    The string identifying the peer you previously requested to connect to.

Returns:

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

Parameters:

  • peerID (String)

    The string that identifies the peer to connect to.

  • timeout (NSTimeInterval)

    The amount of time to wait before canceling the connection attempt.

Returns:

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

Parameters:

  • peerID (String)

    The string identifying the peer that initiated the connection to the session.

Returns:

- (Object) disconnectFromAllPeers

Disconnects the session from all connected peers.

Returns:

- (Object) disconnectPeerFromAllPeers(peerID)

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

Parameters:

  • peerID (String)

    A string identifying the peer to disconnect.

Returns:

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

Parameters:

  • peerID (String)

    A string that uniquely identifies a peer.

Returns:

  • (String)

    The name for the peer, or nil if peerID is not associated with a visible peer.

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

Parameters:

  • sessionID (String)

    A unique string that identifies your application. Your sessionID should be the short name of an approved Bonjour service type. If nil, the session uses the application’s bundle identifier to create a sessionID string.

  • name (String)

    A string identifying the user to display to other peers. If nil, the session uses the device name.

  • mode (GKSessionMode)

    The mode the session should run in. See “Session Modes” for possible values.

Returns:

  • (Object)

    An initialized session object, or nil if an error occurred.

- (Array) peersWithConnectionState(state)

Returns a list of peers in the specified connection state.

Parameters:

  • state (GKPeerConnectionState)

    The connection state to search for. See “Connection States” for possible values.

Returns:

  • (Array)

    An array of NSString objects with a peerID string for each peer visible to the session that is currently in the specified connection state. If there are no peers in the specified connection state, this method returns nil.

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

Parameters:

  • data (NSData)

    The bytes to be sent.

  • peers (Array)

    An array of NSString objects identifying the peers that should receive the data.

  • mode (GKSendDataMode)

    The mechanism used to send the data.

  • error (Pointer)

    If the data could not be queued, an NSError object describing the error.

Returns:

  • (Boolean)

    YES if the data was successfully queued for transmission; NO if the session object was unable to queue the data.

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

Parameters:

  • data (NSData)

    The bytes to be sent.

  • mode (GKSendDataMode)

    The mechanism used to send the data.

  • error (Pointer)

    If the data could not be queued, an NSError object describing the error.

Returns:

  • (Boolean)

    YES if the data was successfully queued for transmission; NO if the session object was unable to queue the data.

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

Parameters:

  • handler (Object)

    The object you want the session to call when it receives data from other peers.

  • context (Object)

    Arbitrary data to be passed to each invocation of the handler.

Returns: