在iOS7中我应该使用什么而不是弃用的GKLeaderboardViewController?

时间:2013-10-02 17:18:44

标签: objective-c ios7 xcode5

我更新了我的IOS 7应用程序,游戏中心有一些不赞成的东西,如加载和解雇排行榜和成就我如何解决它说GKLeaderboardViewController已被弃用

- (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);
}
}

我正在报告这样的分数

- (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);
}
}

1 个答案:

答案 0 :(得分:23)

iOS 7将排行榜,成就等控制器组合到GKGameCenterViewController类中。您可以使用viewState参数来控制要显示的视图。

你会想要做这样的事情来呈现/解雇排行榜:

- (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];
}

同样,为了展示成就,你可以这样做:

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

报告得分看起来像这样:

- (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);
            }
        }];
    }
}