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:
Authenticate the local player (see Authenticating the Local Player in Game Center).
Allocate and initialize an object of type
GKAchievement
. Allocate the object using theinitWithIdentifier:
initialization method or simply use theinit
method but later use thesetIdentifier:
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).Use the
setPercentComplete:
instance method of the achievement object to set the completion value of the achievement (a value between 0 to 100 percent).Call the
reportAchievementWithCompletionHandler:
instance method of the achievement object and pass a block object that returnsvoid
and accepts a parameter of typeNSError
.
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.