3

We present the GameCenter login screen upon the game's launch. At the end of the game, we show a GameCenter button that lets users view their achievements and the game's leaderboards. If they dismissed the original screen and aren't logged in, how can we present the login screen again? Here's the code we're using, but it's not working.

override func viewDidLoad() {
    super.viewDidLoad()

    // Configure view
    let skView = view as! SKView
    skView.multipleTouchEnabled = false
    //skView.showsNodeCount = true
    //skView.showsFPS = true

    // Show intro scene
    let introScene = IntroScene(size: skView.bounds.size, controller: self)
    introScene.scaleMode = .AspectFill
    skView.presentScene(introScene)

    // Authenticate GameCenter player
    authenticateGameCenterPlayer()

}


private func authenticateGameCenterPlayer() {
    var localPlayer = GKLocalPlayer.localPlayer()
    localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
        if ((viewController) != nil) {
            self.presentViewController(viewController, animated: true, completion: nil)
        } else {
            println((GKLocalPlayer.localPlayer().authenticated))
        }
    }
}


func showLeaderboard() {
    // User logged into GameCenter?
    if (!GKLocalPlayer.localPlayer().authenticated) {
        println("Local player not authenticated")
        authenticateGameCenterPlayer()
        return
    }

    // If here, user is authenticated so present leaderboards
    var gcViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self
    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
    gcViewController.leaderboardIdentifier = "highScoresLeaderboard"
    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
}
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • I don't believe it is possible as `GameKit` handles this. Check this answer: http://stackoverflow.com/a/21132075/2108547 – Daniel Storm Jun 03 '15 at 00:14

3 Answers3

2

I understand Apple documentation mentions this in the the Game Center Programming Guide under Common Tasks When Working with Players > Authenticating a Local Player on the Device.

Important: Game Kit handles opting out of Game Center across all games that support Game Center. If a player has already declined to create an account, when your game authenticates the player, it is told there is no authenticated player. The player never sees an authentication dialog. Because Game Kit handles this process across all games, your game should not include its own mechanism to disable Game Center authentication or ask a player’s permission to authenticate. Instead, your game should simply authenticate the player every time it launches and respond appropriately when authentication completes

.

zardon
  • 1,601
  • 4
  • 23
  • 46
1

Game Center remembers the users log-in preferences and if the user dismisses the log-in dialog too many times, it will stop being displayed, even when you call localPlayer.authenticateHandler

The recommended way of handling this is to display a message telling the user to log in through the game center app

ryan.tait
  • 79
  • 1
1

Almost a year late, but I encountered a similar issue and implemented a type of work around.

Check to see if the user is authenticated, and if not direct them to use the game center deeplink. This will prompt the login screen.

UIApplication.sharedApplication().openURL(NSURL(string: "gamecenter:")!)

If a user chooses not to login, they will be redirected back to your app. If a user logs in, they will have the ability to navigate back to your app via the back "Back to App" button at the top.

Ray
  • 894
  • 1
  • 6
  • 23