chanceofthat
chanceofthat

Reputation: 457

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.

Upvotes: 3

Views: 1535

Answers (3)

CrazyJoeLv
CrazyJoeLv

Reputation: 570

Exactly the same happens for me. Tried on multiple devices with the same result. Deleting app/changing Game Centre setting won't help. Your code looks fine though. It looks like GC for Swift is totally broken..

Upvotes: 0

Jon
Jon

Reputation: 101

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.

Upvotes: 10

chanceofthat
chanceofthat

Reputation: 457

I tried running my app in simulator and the authentication window came up fine. I am not sure why it is not working on my device however.

Upvotes: 0

Related Questions