我创建了一个使用Game Center
和的应用
这是我添加到文件中的代码:
func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
}
else {
println((GKLocalPlayer.localPlayer().authenticated))
println(error)
}
}
}
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)
}
func saveHighscore(score:Int) {
//check if user is signed in
if GKLocalPlayer.localPlayer().authenticated {
var scoreReporter = GKScore(leaderboardIdentifier: "countrymasterlbid20151993") //leaderboard id here
scoreReporter.value = Int64(score) //score variable here (same as above)
var scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: {(error : NSError!) -> Void in
if error != nil {
println("error")
}
})
}
}
@IBAction func gc(sender: AnyObject) {
saveHighscore(score)
showLeader()
}
在ViewDidLoad
基金中我添加了这段代码:
authenticateLocalPlayer()
当我运行simulator
时,它没有显示connection view
和error
消息:
错误域= GKErrorDomain代码= 2“请求的操作已经执行 用户取消或禁用。“UserInfo = 0x7fcee382af30 {NSLocalizedDescription =请求的操作已被取消或 被用户禁用。}