Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Game Center Authentication Handler view controller is always nil

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.

like image 898
chanceofthat Avatar asked Oct 27 '25 10:10

chanceofthat


1 Answers

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.

like image 150
Jon Avatar answered Oct 30 '25 00:10

Jon