How to open GameCenter in tvOS

Deadly 提交于 2019-12-09 12:58:13

问题


How can I open a game center leaderboard in tvOS? I've used this code for my iPhone games, 'leaderboardIdentifier' aren't available on tvOS.

I've planned to use the same leaderboard on the AppleTV (it will be the same game).

Many thanks for your help, Stefan

    @IBAction func handleGameCenter(sender: UIButton) {
        let gcViewController = GKGameCenterViewController()
        gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
        gcViewController.leaderboardIdentifier = gamePrefix + "Leaderboard"
        gcViewController.gameCenterDelegate = self

        // Show leaderboard
        self.presentViewController(gcViewController, animated: true, completion: nil)
    }

    func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
        gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
    }

回答1:


I also had the problem with "No data available" screen but finally solved it. This worked for me to open gamecenter leaderboard on tvOS:

  1. open Assets.xcassets (same file where you set your app icon/launchscreen)
  2. right click in the panel with appicon/launchsreen and select Game Center -> New Apple TV Leaderboard
  3. add graphics for the new leaderboard
  4. while leaderboard is selected in assets file on the right side panel find Identifier field and put identifier of your leaderboard there
  5. use this code to open the leaderboard:

    GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];
    gcViewController.gameCenterDelegate = self;
    [self presentViewController:gcViewController animated:YES completion:nil];
    



回答2:


Just this seems to work:

GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
if (gameCenterController != nil)
{
    gameCenterController.gameCenterDelegate = self;
    [self presentViewController: gameCenterController animated: YES completion:nil];
}



回答3:


.viewState and .leaderboardIdentifier are not available on tvOS, so you can open the GC controller with that code, but the page will say "No data available".



来源:https://stackoverflow.com/questions/32770956/how-to-open-gamecenter-in-tvos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!