Retrieving game center achievement by identifier

只谈情不闲聊 提交于 2019-12-07 07:36:28

问题


I have a question related to Game center achievements. Is it possible to retrieve the name/description of an achievement by its identifier? I am trying to avoid hardcoding every identifier with its corresponding name, so is there a solution that can get the name? thanks, Sami


回答1:


Of course, that's exactly why you have localization support for achievements (as well as leaderboards) inside iTunes Connect.

However, there is no way to ask Game Center about localized info for just one achievement based on it's ID. Instead, you ask for info about all achievements which gives you an array of GKAchievementDescription objects which would be best to put into a dictionary where keys are achievement IDs and then you select a proper GKAchievementDescription object from that dictionary.

NSMutableDictionary *achievementDescriptions = [[NSMutableDictionary alloc] init];
[GKAchievementDescription loadAchievementDescriptionsWithCompletionHandler:^(NSArray *descriptions, NSError *error) {
    if (error != nil) {
        NSLog(@"Error getting achievement descriptions: %@", error);
    }
    for (GKAchievementDescription *achievementDescription in descriptions) {
        [achievementDescriptions setObject:achievementDescription forKey:achievementDescription.identifier];
    }
}];

And then when you want to display info for some achievement:

GKAchievementDescription *achievementDescription = [achievementDescriptions objectForKey:currentAchievement.identifier];

This object gives you a localized title, description for when it is achieved and unachieved, as well as number of points it awards and an image you specified in iTunes Connect.



来源:https://stackoverflow.com/questions/6949005/retrieving-game-center-achievement-by-identifier

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