How to reauthenticate Game Center?

79 Views Asked by At

I have 2 languages on my game and I have 2 leaderboards on iTunes Connect. When I switch languages I want Game Center to reauthenticate and change the leaderboard on the fly. My code works because if I close the app with home button and reopen it (not quitting from app switcher) it loads the other leaderboard's data. But it does not load the new data without closing/reopening. I think I have to reauthenticate somehow but calling the same authentication code does not do the job.

func authenticateLocalPlayer(after: Double) {
    DispatchQueue.main.asyncAfter(deadline: .now() + after) {
        localPlayer = GKLocalPlayer.localPlayer()
        localPlayer.authenticateHandler = {(ViewController, error) -> Void in
            if((ViewController) != nil) {
                // Show login if player is not logged in
                //self.present(ViewController!, animated: true, completion: nil)
            } else if (localPlayer.isAuthenticated) {
                // Get the default leaderboard ID
                localPlayer.loadDefaultLeaderboardIdentifier(completionHandler: { (leaderboardIdentifer, error) in
                    if error != nil { print(error ?? "Error")
                    } else { gcDefaultLeaderBoard = leaderboardIdentifer! }
                })

                leaderboardRequest?.identifier = NSLocalizedString("Leaderboard ID".localized(lang: UserDefaults.standard.object(forKey: "Language") as! String), comment: "")
           }
        }
    }
}
1

There are 1 best solutions below

0
On

Adding these solved the issue.

leaderboardRequest?.identifier = "XXXX"
localPlayer.setDefaultLeaderboardIdentifier("XXXX", completionHandler: { (err) in })