How Do you Add Game Center Leaderboards and achievements to a iOS Application in Xcode 6 using the gaming technology Sprite Kit with the programming language set to swift?
1
votes
1 Answers
1
votes
heres my game center code. you will put this in your gamescene code. you have to create a leader board in itunes connect and your leaderboardName must match the one you put in the code. (i called it "leaderboardName"). Also very important to add this delegate at the top of your class GKGameCenterControllerDelegate
//MARK: GameCenter
//send high score to leaderboard
func saveHighscore(score:Int) {
//check if user is signed in
if GKLocalPlayer.localPlayer().authenticated {
var scoreReporter = GKScore(leaderboardIdentifier: "leaderboardName") //leaderboard id here
scoreReporter.value = Int64(score) //score variable here (same as above)
var scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, {(error : NSError!) -> Void in
if error != nil {
println("error")
}
})
}
}
//shows leaderboard screen
func showLeader() {
var vc = self.view?.window?.rootViewController
var gc = GKGameCenterViewController()
gc.gameCenterDelegate = self
vc?.presentViewController(gc, animated: true, completion: nil)
}
//hides leaderboard screen
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
//initiate gamecenter
func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
let vc: UIViewController = self.view!.window!.rootViewController!
vc.presentViewController(viewController, animated: true, completion: nil)
}
else {
println((GKLocalPlayer.localPlayer().authenticated))
}
}
}