Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I use instead of deprecated GKLeaderboardViewController in iOS7?

ive updated my app for IOS 7 and game center has a few things deprecated such as loading and dismissing the leaderboard and achievements how can i fix them it says GKLeaderboardViewController is deprecated

- (IBAction)LeaderBoardsButton:(id)sender {
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
    leaderboardController.leaderboardDelegate = self;
    [self presentViewController:leaderboardController animated:YES completion:NULL];
}
{
    AudioServicesPlaySystemSound(SoundID);
}
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[self dismissViewControllerAnimated:YES completion:NULL];
{
    AudioServicesPlaySystemSound(SoundID2);
}
}

- (IBAction)AchievementsButton:(id)sender {
GKAchievementViewController *achievements = [[GKAchievementViewController
                                              alloc] init];
if (achievements != nil)
{
    achievements.achievementDelegate = self;
    [self presentViewController:achievements animated:YES completion:NULL];
}
{
    AudioServicesPlaySystemSound(SoundID);
}
}

- (void)achievementViewControllerDidFinish:(GKAchievementViewController
                                        *)viewController
{
[self dismissViewControllerAnimated:YES completion:NULL];
{
    AudioServicesPlaySystemSound(SoundID2);
}
}

i am reporting the score like this

- (IBAction)ShareScore:(id)sender {
[self.gameCenterManager reportScore: counter forCategory: self.currentLeaderBoard];

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
    leaderboardController.category = self.currentLeaderBoard;
    leaderboardController.timeScope = GKLeaderboardTimeScopeToday;
    leaderboardController.leaderboardDelegate = self;
    [self presentViewController:leaderboardController animated:YES completion:NULL];
}
{
    AudioServicesPlaySystemSound(SoundID);
}
}
like image 788
Azabella Avatar asked Nov 28 '22 15:11

Azabella


1 Answers

iOS 7 combines the leaderboards, achievements, etc. controllers together into the GKGameCenterViewController class. You use the viewState parameter to control which view you want displayed.

You'll want to do something like this to present/dismiss the leaderboards:

- (void) presentLeaderboards {
    GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
    gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
    gameCenterController.gameCenterDelegate = self;
    [self presentViewController:gameCenterController animated:YES completion:nil];
}

- (void) gameCenterViewControllerDidFinish:(GKGameCenterViewController*) gameCenterViewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Similarly, for presenting achievements, you can do this:

- (void) presentAchievements {
    GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
    gameCenterController.viewState = GKGameCenterViewControllerStateAchievements;
    gameCenterController.gameCenterDelegate = self;
    [self presentViewController:gameCenterController animated:YES completion:nil];
}

Reporting score would look something like this:

- (void) reportHighScore:(NSInteger) highScore forLeaderboardId:(NSString*) leaderboardId {
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        GKScore* score = [[GKScore alloc] initWithLeaderboardIdentifier:leaderboardId];
        score.value = highScore;
        [GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"error: %@", error);
            }
        }];
    }
}
like image 66
Greg Avatar answered Mar 17 '23 02:03

Greg