Reporting Achievements to Game Center

Problem

You created achievements for your game in iTunes Connect and you are ready to use them in your game.

Solution

Use the GKAchievement class in your iOS app.

Discussion

Reporting achievement progress to Game Center is similar to reporting scores to leaderboards in Game Center (see Reporting Scores to Leaderboards). Follow these steps to report an achievement to Game Center:

  1. Authenticate the local player (see Authenticating the Local Player in Game Center).

  2. Allocate and initialize an object of type GKAchievement. Allocate the object using the initWithIdentifier: initialization method or simply use the init method but later use the setIdentifier: instance method of the achievement to set its identifier. The identifier of an achievement is the Achievement ID that we selected when creating the achievement (see Creating Achievements in iTunes Connect).

  3. Use the setPercentComplete: instance method of the achievement object to set the completion value of the achievement (a value between 0 to 100 percent).

  4. Call the reportAchievementWithCompletionHandler: instance method of the achievement object and pass a block object that returns void and accepts a parameter of type NSError.

The following sample code reports 50 percent completion on an achievement with ID of MGL1HP1C (see Creating Achievements in iTunes Connect):

- (BOOL) reportAchievementWithID:(NSString *)paramAchievementID percentageCompleted:(double)paramPercentageCompleted{ BOOL result = NO; if ([paramAchievementID ...

Get Writing Game Center Apps in iOS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.