Game Center - handling failed achievements?

Im noob in the game center @ games in general. I made a second game now and implemented a game center.

If the Internet is available, there are no problems, everything works well. But now I deliberately make the Internet inaccessible, and when I get the achievement, it is obvious that it is not registered in the Game Center Achievement.

How and how best to deal with this problem?

Thanks....

+1
source share
5 answers

You can add GKAchievement objects that are not registered in the array, and then resend them when you return to the connection. You should also consider transferring this array to persistent storage if the application terminates before it can send these achievements. Try this in the completion handler:

 // Load or create array of leftover achievements if (achievementsToReport == nil) { achievementsToReport = [[NSKeyedUnarchiver unarchiveObjectWithFile:pathForFile(kAchievementsToReportFilename)] retain]; if (achievementsToReport == nil) { achievementsToReport = [[NSMutableArray array] retain]; } } @synchronized(achievementsToReport) { if(error == nil) { // Achievement reporting succeded // Resend any leftover achievements BOOL leftoverAchievementReported = NO; while ([achievementsToReport count] != 0) { [self resendAchievement:[achievementsToReport lastObject]]; [achievementsToReport removeLastObject]; leftoverAchievementReported = YES; } // Commit leftover achievements to persistent storage if (leftoverAchievementReported == YES) { [NSKeyedArchiver archiveRootObject:achievementsToReport toFile:pathForFile(kAchievementsToReportFilename)]; } } else { // Achievement reporting failed [achievementsToReport addObject:theAchievement]; [NSKeyedArchiver archiveRootObject:achievementsToReport toFile:pathForFile(kAchievementsToReportFilename)]; } } 

Hope this helps.

+2
source

I see that the two answers here focus on specific mechanisms for archiving messages about undeliverable achievements. For a higher level of description of the general approach, you can see my answer to this related question: Reliable game center Achieving the code

+1
source

Achievements (and all gamecenter materials, such as leader updates) are NSCoding compliant. You can save them if you receive an error message and send them later. This is what the apple recommends in its documents.

Your application should handle errors when it fails to report progress in Game Center. For example, a device might not have a network when you tried to report progress. The correct way to deal with network errors is to save the achievement object (perhaps add it to the array). Then, your application should periodically try to report progress until it is successfully reported. The GKAchievement class supports the NSCoding protocol, which allows your application to archive the achievement object when it moves to the background.

from: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/Achievements/Achievements.html#//apple_ref/doc/uid/TP40008304-CH7-SW13

0
source
  // Submit an achievement to the server and store if submission fails - (void)submitAchievement:(GKAchievement *)achievement { if (achievement) { // Submit the achievement. [achievement reportAchievementWithCompletionHandler: ^(NSError *error) { if (error) { // Store achievement to be submitted at a later time. [self storeAchievement:achievement]; } else { NSLog(@"Achievement %@ Submitted..",achievement); if ([storedAchievements objectForKey:achievement.identifier]) { // Achievement is reported, remove from store. [storedAchievements removeObjectForKey:achievement.identifier]; } [self resubmitStoredAchievements]; } }]; } } 
0
source

If someone stumbles upon this question in the future, Apple now has sample code for presenting achievements, which includes a way to archive achievements that could not be sent (due to lack of network connectivity, etc.). You will find it in the Game Center Programming Guide .

0
source

Source: https://habr.com/ru/post/1369355/


All Articles