显示排行榜崩溃应用程序,因为不支持showViewController

时间:2015-06-03 02:33:24

标签: ios swift ios7 ios8 game-center-leaderboard

在iOS 7中展示Game Center排行榜的正确方法是什么?下面的代码在showViewController崩溃,因为它只支持iOS 8.其他Stack Overflow帖子建议在iOS 7中使用presentViewController,但这只显示一个空白的排行榜。

func showLeaderboards() {
    // User logged into GameCenter?
    if (!GKLocalPlayer.localPlayer().authenticated) {
        println("Local player not authenticated")
        // Show message
        return
    }

    // If here, user authenticated. Present leaderboards.
    var gcViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self
    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
    gcViewController.leaderboardIdentifier = HighScoresLeaderboardKey
    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
}

1 个答案:

答案 0 :(得分:1)

如果您的应用定位到iOS 7.x,则应使用以下代码:

self.presentViewController(gcViewController, animated: true, completion: nil)

而不是:

self.showViewController(gcViewController, sender: self)