Swift Display game center leaders
I am currently trying to create a leaderboard that I created. The player is authenticated just fine, but when the Game Center window opens, it's very strange. Here's a picture:
Here's the code I'm using to display this image:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.showLeaderboard()
}
func showLeaderboard() {
var leaderView = UIViewController()
var leaderViewController = GKGameCenterViewController(rootViewController: leaderView)
leaderViewController.viewState = GKGameCenterViewControllerState.Leaderboards
leaderViewController.leaderboardIdentifier = "High_Score_Board"
self.showViewController(leaderViewController, sender: self)
//self.presentViewController(leaderViewController, animated: true, completion: nil)
}
func leaderboardViewControllerDidFinish(controller: GKGameCenterViewController){
controller.dismissViewControllerAnimated(true, completion: nil)
}
It's all in my GameViewController. Also, even if it does work, how do I access this method in my SKScenes? Thanks for the help!
source to share
Import GameKit:
import GameKit
Make sure to add the delegate GKGameCenterControllerDelegate
to your class.
class ViewController: UIViewController, GKGameCenterControllerDelegate {
...
}
This delegate requires a method to be called when the player clicks the Finish button.
func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
This is the function that includes the code needed to display the leaderboard:
func showLeaderboard() {
var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self
gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
// Remember to replace "Best Score" with your Leaderboard ID (which you have created in iTunes Connect)
gcViewController.leaderboardIdentifier = "Best_Score"
self.showViewController(gcViewController, sender: self)
self.navigationController?.pushViewController(gcViewController, animated: true)
// self.presentViewController(gcViewController, animated: true, completion: nil)
}
You can now call showLeaderboard
by clicking the button:
@IBAction func buttonShowLeaderboard(sender: AnyObject) {
showLeaderboard()
}
source to share