I am trying to set up Game Center in a swift sprite kit game. The following is done in my App Delegate
func authenticateLocalPlayer(){
    let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()
    localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
        //handle authentication
        if (viewController != nil){
            self.window?.rootViewController?.presentViewController(viewController, animated: true, completion: nil)
        }
        else{
            if (localPlayer.authenticated){
                gameCenterEnabled = true
                //Get the default leaderboard identifier.
                localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifierr: String!, error: NSError!) -> Void in
                    if (error != nil){
                        NSLog("%@", [error.localizedDescription])
                    }
                    else{
                        leaderboardIdentifier = leaderboardIdentifierr
                    }
                })
            }
            else{
                gameCenterEnabled = false
            }
        }
    }
}
The problem I am having is that the localPlayer.autheniticateHandler always returns a nil viewController even though my local player is not authenticated. Please let me know what I am doing wrong and how to fix this.
I was having the same problem.
Try deleting the app from the device then go to the device's settings under game centre and enable sandbox.
Then run your game again on the device and it should display the game centre login window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With