Stuart Breckenridge

Game Center Authentication and Swift

Update: I’ve published my Swift class that handles Game Center authentication in this gist.

I’ve been rewriting my Game Center authentication code in Swift and come across a strange issue – the GKLocalPlayer().authenticated variable is always false, even after authenticating. The Swift version of the authentication code is below:

func authenticateLocalPlayer()
    {
        self.delegate?.willSignIn()

        self.localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in

            if (error)
            {
                self.delegate?.failedToSignInWithError(error)
            }

            if (viewController != nil)
            {
                dispatch_async(dispatch_get_main_queue(), {
                    self.showAuthenticationDialogueWhenReasonable(viewController)
                    })
            }

            else
            {
                if (self.localPlayer.authenticated == true)
                {
                    println("Player is Authenticated")
                    self.registerListener()
                    self.downloadCachedMatches()
                    self.delegate?.didSignIn()
                }

                else
                {
                    println("User Still Not Authenticated")
                    self.delegate?.failedToSignIn()
                }
            }
        }
    }

The code always runs to User Still Not Authenticated. However, I’ve tested downloading data from Game Center at this point (e.g. leader boards), and it allows the download to happen. (On the other hand, if you dismiss the Authentication Dialogue and then attempt to download leader boards, an error is generated saying the player is not authenticated.)

I can’t see anything wrong with the code, so I assume it’s a bug (Radar) and not an error on my part (though, there’s every chance it is).

N.B. I’ve posted this question to Stack Overflow.


Supported by