我的代码如下:
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并只调用此函数),但是,如果提示我登录并点击"取消&#34 ;,然后再次单击该按钮,它不起作用。 (意味着不会出现登录页面)。有任何想法吗?谢谢你的帮助!