I haven't been able to follow other tutorials and answers on Stack Overflow, so how do I implement GameCenter Leaderboards in my game, in SpriteKit in Swift? What is the code that one would use, and would it go in the GameScene or the GameViewController? I have already set up a leaderboard on iTunes Connect. How would I go about this? Thanks!
First you must authenticate the player:
var localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
}else{
println((GKLocalPlayer.localPlayer().authenticated))
}
}
Then you can open the leaderboard:
func showLeaderboard() {
var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self
gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
gcViewController.leaderboardIdentifier = "YOUR_BOARD_NAME"
self.showViewController(gcViewController, sender: self)
self.navigationController?.pushViewController(gcViewController, animated: true)
}
func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
I suggest doing these in your view controller. Authenticate the player on viewDidAppear though not viewDidLoad
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With