3

I've been stumped on this for quite a long time. I understand how to unlock an achievement in Game Center and I even got a whole messaging system working. But I can't figure out how to check if an achievement has already been unlocked :(

Apparently this doesn't work:

GKAchievement *achievement = [[GKachievement alloc] initWithIdentifier:ident] autorelease]; 
NSLog(@"%i",achievement.completed);

It always traces "0".

Unlocking an achievement does work:

GKAchievement *achievement = [[GKachievement alloc] initWithIdentifier:ident] autorelease]; 
achievement.percentComplete = 100;

So it's not that I made a mistake in the whole achievement thingy, it's just that GameKit can't tell me if the achievement has already been unlocked or not.

I'd be very grateful if someone could help me with this!

Bijoy Thangaraj
  • 5,434
  • 4
  • 43
  • 70

2 Answers2

4

to load the previously submitted achievements for the currently logged user you need to call:

[GKAchievement loadAchievementsWithCompletionHandler: ^(NSArray *scores, NSError *error)
{
    if(error != NULL) { /* error handling */ }
    for (GKAchievement* achievement in scores) {
        // work with achievement here, store it in your cache or smith
    }

}];

You know the absolutely best way to start with Game Center - achievements and high scorers is to have a look at the demo project Apple has online here: http://developer.apple.com/library/ios/#samplecode/GKTapper/Introduction/Intro.html

Have a look at the code - it's simple enough to quickly grasp what's going on and it features local achievement cache, submitting to different leader boards, etc. etc.

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73
0

Am about to start implementing this myself.

From what I read of the docs what I think what you need to do is call

loadAchievementsWithCompletionHandler: 

http://developer.apple.com/library/ios/documentation/GameKit/Reference/GKAchievement_Ref/Reference/Reference.html#//apple_ref/doc/uid/TP40009959-CH1-SW1

brindy
  • 4,585
  • 2
  • 24
  • 27
  • Just to confirm, this is exactly how to do this. I've got a GameKitHelper class which manages GK interactions, including achievements. Works pretty well. – brindy Jan 26 '11 at 01:12