GameCenter not working, authentication and leaderboard not showing
With Swift 2 GameCenter doesn't work for me. ViewController authentication not showing ... Here is my func authenticateLocalPlayer()
:
func authenticateLocalPlayer() {
var localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController: UIViewController?, error: NSError?) -> Void in
if (viewController != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
print("Not Authenticated. ")
} else {
print("Authenticated. ")
}
}
}
It returns "Not Authenticated" every time, but does not represent the ViewController. Any solution?
+3
source to share
1 answer
This solution represents the viewController correctly using Swift 2 in Xcode 7.0.
Note that I changed the code before the start of the if statement. I believe the syntax may have changed in a recent software update as I also had this problem.
In my application, I called authenticateLocalPlayer () in the viewDidLoad () method of the GameViewController class.
func authenticateLocalPlayer() {
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
}
else {
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
+1
source to share