Check if you've already unlocked an achievement in Game Center/GameKit

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 17:35:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!