How to display Leaderboard Sets in iOS
Asked Answered
M

2

9

I was trying to get this to work all last night but it wouldnt. Can anyone help?

I use the following code to display a leaderboard: gameCenterViewController.leaderboardIdentifier = leaderboardId

This works fine for normal leaderboards, but fails to load any leaderboard sets, when I use the leaderboardSetId. Can you link directly to leaderboard sets and if so how do you do this?

Thanks.

Motorize answered 5/5, 2017 at 8:21 Comment(0)
P
1

Please do refer below code and cross verify your configuration.

Hope this would help you.

import GameKit
class YourViewController: UIViewController, GKGameCenterControllerDelegate {

var gcEnabled = Bool() // Stores if the user has Game Center enabled
var gcDefaultLeaderBoard = String() // Stores the default leaderboardID

override func viewDidLoad() {
    super.viewDidLoad()

 self.authenticateLocalPlayer()

}

func authenticateLocalPlayer()
{
    let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()

    localPlayer.authenticateHandler = {(ViewController, error) -> Void in
        if((ViewController) != nil) {

            // 1 Show login if player is not logged in

            self.presentViewController(ViewController!, animated: true, completion: nil)

        } else if (localPlayer.authenticated) {

            // 2 Player is already euthenticated & logged in, load game center

            self.gcEnabled = true

            // Get the default leaderboard ID
            localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifer: String?, error: NSError?) -> Void in
                if error != nil {
                    print(error)
                } else {
                    self.gcDefaultLeaderBoard = leaderboardIdentifer!
                }
            })


        } else {
            // 3 Game center is not enabled on the users device
            self.gcEnabled = false
            print("Local player could not be authenticated, disabling game center")
            print(error)
        }

    }
}


    @IBAction func clickToLeaderBoard(sender: UIButton) {

        let gcVC: GKGameCenterViewController = GKGameCenterViewController()
        gcVC.gameCenterDelegate = self
        gcVC.viewState = GKGameCenterViewControllerState.Leaderboards
        gcVC.leaderboardIdentifier = "YourLeaderboardId"
        self.presentViewController(gcVC, animated: true, completion: nil)

    }

func saveScoreOnGameCenter()
{
    let leaderboardID = "YourLeaderboardId"
    let sScore = GKScore(leaderboardIdentifier: leaderboardID)
    sScore.value = Int64(10)

    GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in
        if error != nil {
            print(error!.localizedDescription)
        } else {
            print("Score submitted")

        }
    })
}

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

}

Update

Also please do check your leaderboard configuration from back end.

Some good posts are here.

http://www.appcoda.com/ios-game-kit-framework/

https://developer.apple.com/game-center/

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/GameKit_Guide/GameCenterOverview/GameCenterOverview.html

Punster answered 17/5, 2017 at 9:27 Comment(2)
Ive got the normal leaderboards showing its just the leaderboard sets that dont show. I tried replacing the leaderboardIds with the leaderboard set IDs in my code but that didnt work. I think this solution assumes that is all you need to do?Motorize
Yes, this is minimum needs to do, can you try integrating my code i have given here and check.Punster
R
-2

Having ensured that you have authenticated the local player and your leaderboards set in iTunes Connect, you will need to report your scores to GameCenter ensuring you use 64bit Int. Now you can add this to display the leaderboard for the local player:

func showGKGameCenterViewController(_ viewController: UIViewController) {
    if gameCenterIsDisabled {
      print("Local player is not authenticated")
    }
    let gameCenterViewController = GKGameCenterViewController()
    gameCenterViewController.delegate = self
    gameCenterViewController.viewState = GKGameCenterViewControllerState.default

    viewController.presentViewController(gameCenterViewController, animated: true, completion: nil)
}
Rooney answered 17/5, 2017 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.