I am integrating Game Center onto my application, and I've done everything right, it's well but I have a problem. When an user completes an achievement, I wanted to add a notification. And I did manage to do that like this :
GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier:@"theFirstAchievement"];
achievement.percentComplete = sharedApp.firstAchievement = sharedApp.firstAchievement + 50;
[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
if (error != nil)
{
NSLog(@"achievement failed");
}
else
{
NSLog(@"achievement succeded");
}
}];
if (achievement.percentComplete == 100.0)
{
[GKNotificationBanner showBannerWithTitle:@"Achievement Unlocked:" message:@"Master Question Master!" completionHandler:nil];
}
My problem is, I don't know if I'm doing something wrong, but it is supposed to show a notification once, but, if I close my application and reopen, when I complete the achievement again it shows the notification again, even though it was already completed in my game center! What am I doing wrong? Thanks!
Look at this bit:
if (achievement.percentComplete == 100.0)
This means that if the achievement percentage completion is 100%, show a banner. However, when you open the app again, and it reads the percentage, the percentage is still 100%, so it shows the banner again.
You'll have to keep a record of which achievements have been earned and awarded
I'd suggest something like this:
BOOL awarded = [[NSUserDefaults standardUserDefaults] boolForKey:@"theFirstAchievement"];
if (achievement.percentComplete == 100.0 && !awarded)
{
[GKNotificationBanner showBannerWithTitle:@"Achievement Unlocked:" message:@"Master Question Master!" completionHandler:nil];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"theFirstAchievement"];
[[NSUserDefaults standardUserDefaults] synchronize];
}