Retrieving Achievements Information Programmatically
Problem
You want to retrieve the progress of achievements that have been reported to Game Center for the local authenticated player.
Solution
You need to invoke the loadAchievementsWithCompletionHandler:
class
method of the GKAchievement
class.
Discussion
Just as we can retrieve leaderboard information programmatically (see Retrieving Leaderboards Information Programmatically), we can ask Game Center to provide us with the latest information about the progress the authenticated local player has made on each one of the achievements that we have enabled on our app. To do this, simply follow these steps:
Authenticate the local player (see Authenticating the Local Player in Game Center).
Next, invoke the
loadAchievementsWithCompletionHandler:
class method of theGKAchievement
class. This method accepts one parameter, which must be a block object that returnsvoid
and accepts two parameters. The first parameter is an array of typeNSArray
, which will contain the achievements retrieved from Game Center. The second parameter is an error of typeNSError
, which will contain any error that might happen during this process.You can then enumerate the objects in the array of achievements retrieved from the aforementioned method. Each achievement object will be of type
GKAchievement
.
Here is an example of how we can retrieve achievements programmatically:
- (void) authenticateAndGetAchievements{ GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; ...
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.