Authenticates the local player on the device.
- iOS 4.1–6.0Deprecated
- macOS 10.8–10.8Deprecated
- Mac Catalyst 13.0–13.0Deprecated
- watchOS 2.0–2.0Deprecated
A block to be called when the player has authenticated or when an error occurs.
The block takes the following parameter:
This parameter is
nilif the player successfully authenticated. Otherwise, it contains an error object that describes the error that occurred.
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.