Check if you've already unlocked an achievement in Game Center/GameKit
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 开发者_JAVA百科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!
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.
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
精彩评论