问题
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!
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/4768163/check-if-youve-already-unlocked-an-achievement-in-game-center-gamekit