我的代码如下:
func authenticateLocalPlayer() {
let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(ViewController, error) -> Void in
if((ViewController) != nil) {
self.presentViewController(ViewController, animated: true, completion: nil)
} else if (localPlayer.authenticated) {
print("Local player already authenticated")
self.gcEnabled = true
if self.gcEnabled == true {
self.showLeaderboard()
self.activity.stopAnimating()
}
// Get the default leaderboard ID
localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifer: String!, error: NSError!) -> Void in
if error != nil {
println(error)
} else {
self.gcDefaultLeaderBoard = leaderboardIdentifer
}
})
}
else {
self.gcEnabled = false
print("Local player could not be authenticated, disabling game center")
print(error)
if((ViewController) != nil) {
self.presentViewController(ViewController, animated: true, completion: nil)
}
} } }
func showLeaderboard() {
let gcVC: GKGameCenterViewController = GKGameCenterViewController()
gcVC.gameCenterDelegate = self
gcVC.viewState = GKGameCenterViewControllerState.Leaderboards
gcVC.leaderboardIdentifier = leaderboardIdentifier
self.presentViewController(gcVC, animated: true, completion: nil)
}
第一次单击我的排行榜按钮时,此代码运行良好(我使用@IBAction 并调用此函数),但是,如果提示我登录并单击“取消”,然后再次单击该按钮, 这没用。(表示不出现登录页面)。有任何想法吗?谢谢你的帮助!