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 theinitmethod 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 returnsvoidand 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 ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access