Authentication

Adding Game Center support requires more than simple coding changes. Game Center imposes specific requirements on the design of your game. For example, to implement leaderboards, your game must have a way to quantitatively measure a player's performance. But more than that, Game Center changes how you design and test your game. You aren't simply adding code to your app; you are also configuring assets used by Game Center. These assets are configured separately from your app bundle but are intimately tied to the code in your game. You need to test these pieces to ensure that all of the pieces work correctly together.

To create a Game Center-aware game, you need to understand these basics before you begin writing code.

Incorporating Game Center into Your Game

Here’s a reasonable process to follow when designing a game that supports Game Center:

  1. Decide which Game Center features you plan to support.
  2. Define your game mechanics, keeping in mind the requirements and limitations of Game Center.
  3. Implement your game engine, ignoring Game Center for now. Focus on developing the other aspects of your game, such as your game engine and gameplay. You do this to avoid needing to create an iTunes Connect record earlier than you need to.
  4. When you need to implement Game Center:

    1. Create an iTunes Connect record for your game, using the game’s bundle ID. Enable Game Center in the iTunes Connect record; setting this flag in the iTunes Connect record authorizes the Game Center service to allow your game to connect to it.

    2. Create an explicit App ID using your game’s bundle ID.

    3. Enable Game Center in this App ID; this authorizes the app on the device to contact Game Center’s servers.

    4. Create a new provisioning profile using this new explicit App ID.

    5. Test to ensure you can build and sign your game using this profile.

    6. Enable the Game Kit to your project.

    7. Implement authentication.

    8. Implement other Game Center features.

    9. Test your Game Center features using the Game Center development environment

Game Center Authentication

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. After you set a handler, authentication begins automatically and is repeated when your game moves to the background and then back to the foreground.

If the device does not have an authenticated player, Game Kit passes a view controller to your authenticate handler. When presented, this view controller displays the authentication user interface. Your game should pause other activities that require user interaction (such as your game loop), present this view controller and then return. When the player finishes interacting with it, the view controller is dismissed automatically.

If the authentication process succeeded, the ISN_GKLocalPlayer singleton object’s Authenticated property is set to TRUE and the object’s other properties are set to match those of the connected player.

If the authentication process failed, the  ISN_GKLocalPlayer singleton object’s Authenticated property is set to FALSE and the object’s other properties are cleared.

See an example of how to authenticate the local player:

ISN_GKLocalPlayer.Authenticate((SA_Result result) => {
    if (result.IsSucceeded) {
        Debug.Log("Authenticate is succeeded!");
    }
    else {
        Debug.Log("Authenticate is failed! Error with code: " + result.Error.Code + " and description: " + result.Error.Message);
    }
});

Two common errors that can be received by your game are worth describing in more detail:

  • Receiving a GKErrorGameUnrecognized error means that you have not enabled Game Center for your app in iTunes Connect. Sign in to your iTunes Connect account and verify that your app has Game Center enabled. Also, confirm that the bundle identifier in your Xcode project matches the bundle identifier you assigned to your app in iTunes Connect.
  • Receiving a GKErrorNotSupported error means that the device your game is running on does not support Game Center. You should disable all Game Center related features.

After successful authentication, you may use any of local player filed. The local player is represented as ISN_GKLocalPlayer singleton object. See the example below how to print local player info after authentication:

using SA.IOSNative.GameKit;
...
ISN_GKLocalPlayer player = ISN_GKLocalPlayer.LocalPlayer;
Debug.Log(player.PlayerID);
Debug.Log(player.Alias);
Debug.Log(player.DisplayName);
Debug.Log(player.Authenticated);
Debug.Log(player.Underage);