在离线模式下(例如,打开飞行模式),尝试找出在Game Center中使用成就的最佳方式.
据我所知,iOS 5+中的Game Center负责离线提交的成就和分数.它就像一个代理缓存,并在用户上线时将它们提交到在线游戏中心.考虑到这一点,我在做什么:
在用户身份验证成功时,我加载成就并将它们存储在字典中.
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *gcAchievments, NSError *error)
{
if (error) { ..skipped ..}
//This dictionary will store current achievments, so that we didn't submit them
//once more and didn't show notification.
achievments = [[NSMutableDictionary alloc] initWithCapacity:gcAchievments.count];
//Storing achievments in dictionary
for(GKAchievement *a in gcAchievments)
[achievments setObject:a forKey:a.identifier];
}];
Run Code Online (Sandbox Code Playgroud)
稍后当我提交新成就时,我会在字典中检查成就,如果成就已经完成,则不提交.如果我提交成就,我也会将其添加到achievments字典(内存中),以立即重新表明已经提交了此成就.
GKAchievement *cachedAchievment = [achievments objectForKey:identifier];
if (cachedAchievment && cachedAchievment.percentComplete >= 100)
{
//Already unlocked this achievment.
return;
}
GKAchievement *achievement = …Run Code Online (Sandbox Code Playgroud)