0

What is the right way to present Game Center leaderboards in iOS 7? The code below crashes on showViewController because it only supports iOS 8. Other Stack Overflow posts suggest using presentViewController for iOS 7, but that only shows a blank leaderboard.

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)
}
Quill
  • 2,729
  • 1
  • 33
  • 44
Crashalot
  • 33,605
  • 61
  • 269
  • 439

1 Answers1

1

If your app targets iOS 7.x, you should use the following code:

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

instead of:

self.showViewController(gcViewController, sender: self)
Crashalot
  • 33,605
  • 61
  • 269
  • 439
酷酷的哀殿
  • 1,021
  • 6
  • 18
  • Thanks for the help, but presentViewController shows an empty leaderboard (i.e., no leaderboards tab, no challenges tab). If we use showViewController on an iOS 8 device, it shows a populated leaderboard. Is there something else we need to do with presentViewController so it shows something? – Crashalot Jun 03 '15 at 03:26
  • i am a beginner of GameKit,i am sorry that i can't help you to solve you "shows an empty leaderboard" – 酷酷的哀殿 Jun 03 '15 at 04:16
  • Thanks, but do you know why the result differs when we use presentViewController instead of showViewController for an iOS 8 device? – Crashalot Jun 03 '15 at 04:20
  • Actually seems to work now, thanks! No clue why it didn't work at first. – Crashalot Jun 03 '15 at 04:30