Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在项目中实施排行榜(游戏中心)_Ios_Objective C_Leaderboard - Fatal编程技术网

Ios 如何在项目中实施排行榜(游戏中心)

Ios 如何在项目中实施排行榜(游戏中心),ios,objective-c,leaderboard,Ios,Objective C,Leaderboard,我正在尝试用Objective-C创建一个排行榜,但没有运气。我一直在这里和其他网站上寻找,但我似乎找不到任何有效的。我已经在iTunes Connect上创建了排行榜,但这是我遇到麻烦的代码。我收到一些警告,有些术语已被弃用 这是我使用的代码: - (IBAction)ShowLeaderboard:(id)sender { GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController all

我正在尝试用Objective-C创建一个排行榜,但没有运气。我一直在这里和其他网站上寻找,但我似乎找不到任何有效的。我已经在iTunes Connect上创建了排行榜,但这是我遇到麻烦的代码。我收到一些警告,有些术语已被弃用

这是我使用的代码:

- (IBAction)ShowLeaderboard:(id)sender {

GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController alloc] init];

if (leaderboardController != nil) { leaderboardController.leaderboardCategory = self;

[self presentModalViewController: leaderboardController animated: YES]; }

}

- (IBAction)SubmitScoreToGameCenter:(id)sender {

GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"LeaderboardName"];

scoreReporter.value = HighScoreNumber;

[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) { if (error != nil){ NSLog(@"Submitting score failed"); }

    else { NSLog(@"Submitting score succeeded"); } }];

}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController

{ [self dismissModalViewControllerAnimated:YES];

}
在viewDidLoad中:

{

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) { if (error == nil)

{ NSLog(@"Authentication Successful");

}

    else { NSLog(@"Authentication Failed"); } }];

如果你真的查过这个,你会发现很多关于这个的教程,所以我认为你没有,或者你还没有真正学会如何使用xCode

下面是我如何编写代码:

    -(void)submitScore //submit the score to game centre
    {
        GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:@"LeaderboardName"]; 
        int64_t GameCenterScore = Score;
        score.value = GameCenterScore;

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


-(void)authentication //log player into game centre
{
    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) {
                NSLog(@"authentication succcesful");
                GameCenterAvaliable = YES;
                [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    }
                    else{
                        leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            }

            else{
                NSLog(@"authentication unseuccseful");
                GameCenterAvaliable = NO;
            }
        }
    };
}

-(IBAction)ShowGameCenter:(id)sender //show game centre
{
    GKLeaderboardViewController *LeaderboardController = [[GKLeaderboardViewController alloc] init];
    if (LeaderboardController != nil) {
        LeaderboardController.leaderboardDelegate = self;
        [self presentViewController:LeaderboardController animated:YES];
    }
}

//Animate gc out if finished with it

-(void)leaderboardViewControllerDidFinish: (GKLeaderboardViewController *) viewController{
    [self dismissViewControllerAnimated:YES];
}
这是为了将你的分数发送到排行榜,但我认为你在实际设置排行榜时遇到了麻烦?对
从iTunes Connect转到你的应用程序,从那里转到你想要进入排行榜的应用程序,从那里转到游戏中心。单击添加排行榜,然后选择单个排行榜,然后填写信息。您的排行榜ID是您在代码中输入的名称,例如,我在上面调用了我的排行榜名称。完成后,您可以返回应用程序并向下滚动,直到找到game Center,然后单击加号并添加所选排行榜。然而,如果你甚至不知道如何将应用程序添加到iTunes connect,你应该认真阅读整篇文章,例如,这里是一个很好的地方,可以了解如何理解

@TekayaMarouene,这不是很有帮助。苹果的文档让我困惑。。。不过,谢谢。我已经在iTunes Connect中创建了排行榜,所以我有一个排行榜ID。我只是没有找到如何正确编码的教程,它们不是最新的。如何让用户登录到游戏中心?正如您在上面提供的代码(在viewDidLoad中)中看到的,该代码已被弃用。打开排行榜的代码是什么?我知道,但如果运行它,它会工作得很好,我目前正在应用商店的游戏中使用这种方法!Michael,我刚刚注意到,当我在真实设备上测试我的应用程序时,我没有登录到Game Center。这两款设备都运行iOS 8.1.2(最新版本),如果这与此有关。。。您在您的哪个应用程序中使用此代码?代码已更新,如果您转到设置,然后转到游戏中心,您可能未登录,请检查您的帐户是否以这种方式登录。我已登录。但是,我在同一屏幕底部的Developer下启用了Sandbox。现在,当我启动应用程序时,它会要求我登录游戏中心,就像在模拟器中一样。是这样吗?人们可以自动登录吗?