Apple Game Center

Learn how to link Apple Game Center accounts to Stash

Before you configure account linking, you need to:

  1. Install the Apple Game Kit Unity plugin. The GameKit framework is used to incorporate Apple Game Center features, including player identities.
  2. Set up your App Store Connect app and ensure you have the Game Center capability set up.
  3. Download and import the Stash Plugin for Unity.

1. Add Apple Game Center as an ID provider

In the Web Shop section of Stash Studio, add Apple Game Center as an ID provider and enter your iOS or MacOS app BundleId. Stash needs this to complete the account linking process.

2. Call fetchItems for ID verification

Apple Game Center accounts can be verified and linked to Stash using a signature generated by calling Apple’s fetchItems endpoint. This endpoint returns the following parameters:

  • signature: The verification signature data that GameKit generates.
  • publicKeyURL: The URL for the public encryption key.
  • salt: A random NSString that GameKit uses to compute the hash and randomize it.
  • timestamp: The signature’s creation date and time.

You also need the TeamPlayerID number, which you can fetch locally from Apple Game Center:

private static async Task Login() {
  if (!GKLocalPlayer.Local.IsAuthenticated) {
    // Perform the authentication.
    var player = await GKLocalPlayer.Authenticate();
    Debug.Log($"Login successful - {player.DisplayName}");

    // Extract the team player id locally.
    var localPlayer = GKLocalPlayer.Local;
    TeamPlayerID = localPlayer.TeamPlayerId;

    // Fetch signature, salt, timestamp and public key for server-side
    // verification.
    var fetchItemsResponse = await GKLocalPlayer.Local.FetchItems();
    Signature = Convert.ToBase64String(fetchItemsResponse.Signature);
    Salt = Convert.ToBase64String(fetchItemsResponse.Salt);
    PublicKeyUrl = fetchItemsResponse.PublicKeyUrl;
    Timestamp = fetchItemsResponse.Timestamp.ToString();
  } else {
    Debug.Log("Already logged in.");
  }
}

3. Link accounts

You use Stash's LinkAppleGameCenter method to link the accounts. This method uses the parameters from the previous step, as well as:

  • The internal user ID that's used for purchase identification.
  • The code challenge associated with the linking request.

📘

Stash recommends refreshing credentials before linking to Stash. Credentials older than 60 minutes are considered expired, which causes the linking process to fail.

// Wrap in try-catch block and handle StashRequestError appropriately.
try {
  LinkResponse response = await StashClient.LinkAppleGameCenter(
      _stashChallenge, InternalPlayerId, "com.Stash.iosdemo",
      GameCenter.TeamPlayerID, GameCenter.Signature, GameCenter.Salt,
      GameCenter.PublicKeyUrl, GameCenter.Timestamp);
  // Linking successful, prompt player to return back to the web shop.
} catch (StashRequestError e) {
  Debug.LogError(
      $"[STASH][Apple Game Center] Account linking failed. Code: {e.Code}, Message: {e.Message}");
}

The accounts are now linked and users are returned to the web shop to complete the flow that triggered the linking process.