Created
April 15, 2015 04:23
-
-
Save endodoug/2c8893dabee8319ddf35 to your computer and use it in GitHub Desktop.
Game Center Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GameKit | |
class GameScene: SKScene, GKGameCenterControllerDelegate { | |
var gcEnabled = Bool() | |
var gcDefaultLeaderboard = String() | |
var LeaderboardID = "com.endodoug.MagicalLeaderboard" | |
} | |
override func didMoveToView(view: SKView) { | |
AuthenticateLocalPlayer() | |
} | |
func BestScore() { | |
submitScore() | |
} | |
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!) { | |
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil) | |
} | |
func showLeaderboard() { | |
var gcVC : GKGameCenterViewController = GKGameCenterViewController() | |
gcVC.gameCenterDelegate = self | |
gcVC.viewState = GKGameCenterViewControllerState.Leaderboards | |
gcVC.leaderboardIdentifier = LeaderboardID | |
var vc = self.view?.window?.rootViewController | |
vc?.presentViewController(gcVC, animated: true, completion: nil) | |
} | |
func submitScore() { | |
var sScore = GKScore(leaderboardIdentifier: LeaderboardID) | |
sScore.value = Int64(tryCountBest) | |
let localPlayer : GKLocalPlayer = GKLocalPlayer.localPlayer() | |
GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError!) -> Void in | |
if error != nil { | |
println(error.localizedDescription) | |
} | |
else | |
{ | |
println("score submitted successfully") | |
} | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment