Instance Method

authenticate(completionHandler:)

Authenticates the local player on the device.

Declaration

func authenticate(completionHandler: ((Error?) -> Void)? = nil)

Parameters

completionHandler

A block to be called when the player has authenticated or when an error occurs.

The block takes the following parameter:

error

This parameter is nil if the player successfully authenticated. Otherwise, it contains an error object that describes the error that occurred.

Discussion

When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. The completion handler is always called on the main thread.

Your game should authenticate the player as early as possible after launching, ideally as soon as you can present a user interface to the player. For example, your game may be launched because the player accepted an invitation to join a match or to take a turn in a turn-based match, so you want your game to authenticate the player and process the match invitation as quickly as possible.

If there is not an authenticated player on the device when your game calls this method, Game Kit displays a user interface that allows the player to sign in with their credentials (or to create a new account if he or she has never used Game Center). Your game should pause other activities that require user interaction (such as a real time game loop) before attempting to authenticate the local player.

Game Kit maintains a strong reference to your completion handler even after successfully authenticating a local player. If your game moves into the background, Game Kit automatically authenticates the player again whenever your game moves back to the foreground. Game Kit calls your same completion handler each time it authenticates the local player. Be mindful that in block programming, any Objective-C object referenced inside a block is also strongly referenced by the block until the block is released. Because Game Kit maintains a strong reference to your completion handler until your game terminates, any objects referenced from within your authentication handler are also held indefinitely.

Each time the completion handler is called, the data stored in the local player singleton object may have changed. A new player may have logged into the device or the player may have simply logged out from Game Center. Because of both of these possibilities, your authentication handler must be prepared to update any other objects that assume that a particular player is logged in. For more information, see Authenticating the Local Player in a Multitasking App.

See Also

Deprecated Methods and Properties

var friends: [String]?

An array of NSString objects containing the player identifiers for the local player’s friends.

Deprecated
func loadDefaultLeaderboardCategoryID(completionHandler: ((String?, Error?) -> Void)?)

Loads the category identifier for the local player’s default leaderboard.

Deprecated
func loadFriendPlayers(completionHandler: (([GKPlayer]?, Error?) -> Void)?)

Retrieves a list of player identifiers for the local player’s friends.

Deprecated
func loadFriends(completionHandler: (([String]?, Error?) -> Void)?)

Retrieves a list of player identifiers for the local player’s friends.

Deprecated
func setDefaultLeaderboardCategoryID(String?, completionHandler: ((Error?) -> Void)?)

Sets the category identifier for the local player’s default leaderboard.

Deprecated

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software