Game Center - 提交到非默认排行榜

Game Centre - Submitting to a Non-Default Leaderboard

所以我有一个具有 Game AGame B 的应用程序。

我已经为游戏 A 正确实施了 Game Center(我使用了 AppCoda tutorial,就像目前为止所有游戏一样)。

现在,如果玩游戏 B,我无法提交分数。我需要将分数提交到在 iTunes Connect 中创建的第二个排行榜。

这是我的 ViewController 部分,用于验证用户身份并将标识符用于排行榜等。

ViewController.h:

-(void)authenticateLocalPlayer{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
    if (viewController != nil) {
        [self presentViewController:viewController animated:YES completion:nil];
    }
    else{
        if ([GKLocalPlayer localPlayer].authenticated) {
            _gameCenterEnabled = YES;                                   //added bool indentier to .h

            // Get the default leaderboard identifier.
            [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                if (error != nil) {
                    NSLog(@"%@", [error localizedDescription]);
                }
                else{
                    _leaderboardIdentifier = leaderboardIdentifier;     //added pointer to NSString to .h
                }
            }];
        }

        else{
            _gameCenterEnabled = NO;
        }
    }
};

}

似乎我的游戏 B 视图控制器 scorings/submitting 就像游戏 A,我想我可以将上面的代码更改为:(允许第二个标识符):

-(void)authenticateLocalPlayer{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
    if (viewController != nil) {
        [self presentViewController:viewController animated:YES completion:nil];
    }
    else{
        if ([GKLocalPlayer localPlayer].authenticated) {
            _gameCenterEnabled = YES;                                   //added bool indentier to .h
            _gameCenterEnabled2 = YES;

            // Get the default leaderboard identifier.
            [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                if (error != nil) {
                    NSLog(@"%@", [error localizedDescription]);
                }
                else{
                    _leaderboardIdentifier = leaderboardIdentifier;     //added pointer to NSString to .h
                }
            }];

            // Get the second leaderboard identifier.
            [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier2, NSError *error) {

                if (error != nil) {
                    NSLog(@"%@", [error localizedDescription]);
                }
                else{
                    _leaderboardIdentifier2 = leaderboardIdentifier2;     //added pointer to NSString to .h
                }
            }];


        }

        else{
            _gameCenterEnabled = NO;
            _gameCenterEnabled2 = NO;
        }
    }
};

}

但出于某种原因,它不会将分数发送到第二个排行榜,而且我找不到任何关于如何将分数提交到 "non-default" 排行榜的 resources/tutorials...

好的,所以我重新阅读了 Apple Doc 并找到了解决方案。

显然只能有一个默认排行榜(在我的问题的代码中经过身份验证和设置)...这不需要像我最初想的那样更改。 (我忘了是用来设置默认板的)

所以我需要做的是将排行榜标识符设置为第二个排行榜的标识符(这将是您在制作第二个排行榜时在 iTunes Connect 中使用的任何 ID)。

我在 Game B View Controller 中报告分数时是这样的:

-(void)reportScore{

//set identifier manually as it's not the default leaderboard
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:@"The_GameB_Leaderboard"];
//GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier2];

score.value = ScoreNumber_B; //Game B HighScore

[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
    if (error != nil) {
        NSLog(@"%@", [error localizedDescription]);
    }
}];
NSLog(@"Reported to Game Center...");

}

不需要更改-(void)authenticateLocalPlayer方法,除非你需要像我一样添加GameB bool,在这种情况下你可以将它添加到GameA Bool下面:

if ([GKLocalPlayer localPlayer].authenticated) {
        _gameCenterEnabled = YES;      //added bool indentier to .h
        _gameCenterEnabled2 = YES;     //GameB bool
    .
    .
    .
}

希望对您有所帮助。