Search code examples
objective-cnsdictionarygame-centernsmutabledictionarygamekit

GameKit GKLocalPlayer alias as NSDictionary key


I am developing an iDevice game and I would like to store the local player's scores on the device. However, the local player may change. For example, if a family shares an iPad, then they may sign in to GameCenter separately before playing the game. To solve this problem, I decided to store the local scores as a dictionary where the keys are determined by the local player and the values are arrays of GKScore objects.

My first idea was to use the GKLocalPlayer shared instance as the key, but since it doesn't conform to NSCopying, GKLocalPlayer is not an ideal key candidate. My second idea was to use the local player's identifier, but Apple states "Do not make assumptions about the contents of the player identifier string. Its format and length are subject to change." Naturally, I am concerned that a player might lose their local scores if their player identifier magically changes one day.

My final idea was to use the local player's alias. I have confirmed that the alias string for an unauthenticated local player is equal to nil. Is it safe to use [NSNull null] as a dictionary key in this case? Is it safe to assume the local player's alias will remain consistent?


Solution

  • The only thing about the player that is guaranteed to be permanent is the playerID string. When it says the format and length are subject to change, that's in the context of trying to interpret the context of the string. AFAIK it isn't that it a particular player's ID might change, but that Apple might generate IDs using a different scheme in the future, so you shouldn't depend on the string having any particular format or certain information being derivable from the string.

    The docs are pretty unambiguous about that being the right way to do it, though:

    Every player account on Game Center is permanently assigned a unique player identifier string. Your game should use this string to store per-player information or to disambiguate between players