Search code examples
authenticationswiftgame-centerios8

Game Center Authentication with Swift


Using the following code, the GKLocalPlayer().authenticated variable is always false. Once the code runs to "User still not authenticated", you are able to download Game Center data. Is this a bug or an issue with the code below?

func notificationReceived()
{
    println("GKPlayerAuthenticationDidChangeNotificationName - Authentication Status: \(self.localPlayer.authenticated)")
}

//MARK: 2 Authenticate the Player
func authenticateLocalPlayer()
{
    println(__FUNCTION__)
    self.delegate?.willSignIn()

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

        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()
        }

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

//MARK: 2a Show Authentication Dialogue
func showAuthenticationDialogueWhenReasonable(viewController:UIViewController!) -> Void
{
    println(__FUNCTION__)
    UIApplication.sharedApplication().keyWindow.rootViewController.presentViewController(viewController, animated: true, completion: nil)
}

The console output look like this:

    init(notification:)
    authenticationCheck()
    authenticateLocalPlayer()
    GKPlayerAuthenticationDidChangeNotificationName - Authentication Status: false
    GKPlayerAuthenticationDidChangeNotificationName - Authentication Status: false
    showAuthenticationDialogueWhenReasonable
    GKPlayerAuthenticationDidChangeNotificationName - Authentication Status: false
    GKPlayerAuthenticationDidChangeNotificationName - Authentication Status: false
    GKPlayerAuthenticationDidChangeNotificationName - Authentication Status: false
    User Still Not Authenticated
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.149128 com.apple.viceroytrace: ENV: VRTraceLogToFile="-"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.149544 com.apple.viceroytrace: ENV: VRTraceErrorLogLevel="ALL"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.149997 com.apple.viceroytrace: ENV: VRTraceMonitorNSLog="1"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.150429 com.apple.viceroytrace: ENV: VRTraceStreamOutputFormat="CSV"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.150875 com.apple.viceroytrace: ENV: VRTraceLogToFile="-"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.151312 com.apple.viceroytrace: ENV: VRTraceErrorLogLevel="ALL"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.151768 com.apple.viceroytrace: ENV: VRTraceMonitorNSLog="1"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.152211 com.apple.viceroytrace: ENV: VRTraceStreamOutputFormat="CSV"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.152626 com.apple.viceroytrace: ENV: VRTraceLogToFile="-"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.153060 com.apple.viceroytrace: ENV: VRTraceErrorLogLevel="ALL"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.153489 com.apple.viceroytrace: ENV: VRTraceMonitorNSLog="1"
    Aug  2 07:33:10 iMac.local   <Debug>: 07:33:10.153925 com.apple.viceroytrace: ENV: VRTraceStreamOutputFormat="CSV"
    Aug  2 07:33:10 iMac.local   <Info>: 07:33:10.154140 com.apple.viceroytrace: [CHECKPOINT] logging-started
    Aug  2 07:33:10 iMac.local   <Notice>: 07:33:10.154146 com.apple.viceroytrace: gVRTraceErrorLogLevel initialized to ALL (9)
    Aug  2 07:33:10 iMac.local   <Info>: 07:33:10.144097 com.apple.AVConference: GKSConnSettings: set server: {
        "gk-cdx" = "17.173.254.218:4398";
        "gk-commnat-cohort" = "17.173.254.220:16386";
        "gk-commnat-main0" = "17.173.254.219:16384";
        "gk-commnat-main1" = "17.173.254.219:16385";
    }

Solution

  • This has been fixed in Xcode 6 Beta 6 – there's now GKLocalPlayer.localPlayer() class func which does the business.