Search code examples
swiftgame-centerleaderboardachievements

Calling achievement screen in Swift


I am having some extreme difficulties calling the achievements screen in game center. I have already set up the achievements in iTunes connect and it pops up fine if I access the achievements screen through the leaderboard first. However; I would like to be able to press a specific achievement button and be directed directly to the achievements screen. Can any one help? I have searched high and low on the internet ( and read through all of the documentation). I have found many resources for implementing leaderboards, but not many resources for implementing achievements in swift. My code is below. Any suggestions for my last two functions?

override func viewDidLoad() {
    super.viewDidLoad()

    login()

}

func login() {
    println("Game Center Login Called")
    let localPlayer = GKLocalPlayer.localPlayer()

    // Handle the authentication
    localPlayer.authenticateHandler = {(Home: UIViewController!, error: NSError!) -> Void in
        if Home != nil {
            println("Authentication is being processed.")
            self.presentViewController(Home, animated: true, completion: nil)

        } else {
            println("Player has been successfully authenticated.")
        }
    }

}

func showLeaderboard() {


    let gkScore = GKScore(leaderboardIdentifier: "high_Score_Leader_Board")
    gkScore.value = Int64(highscore)
    GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
        if (error != nil) {
            // handle error
            println("Error: " + error.localizedDescription);
        } else {
            println("Score reported: \(gkScore.value)")
        }
    }))


    var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    gcViewController.leaderboardIdentifier = "high_Score_Leader_Board"
    self.showViewController(gcViewController, sender: self)
    self.presentViewController(gcViewController, animated: true, completion: nil)
}





@IBAction func gameCenterButtoPressed(sender: AnyObject) {
    showLeaderboard()
}



func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
{
    self.dismissViewControllerAnimated(true, completion: nil)
}

func showAchievements() {
    // show Achievements screen
}


@IBAction func achievementButtonPressed(sender: AnyObject) {

    // Call show achievements function when button pressed
}

Solution

  • Instead of:

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
    

    I think what you'll want is:

    gcViewController.viewState = GKGameCenterViewControllerState.Achievements
    

    And I found this information in this related tutorial.