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 theGKAchievementclass. This method accepts one parameter, which must be a block object that returnsvoidand 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]; ...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