如何在iOS中显示排行榜集

前端之家收集整理的这篇文章主要介绍了如何在iOS中显示排行榜集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让这一切都在昨晚完成,但它不会.有人可以帮忙吗?

我使用以下代码显示排行榜
gameCenterViewController.leaderboardIdentifier = leaderboardId

这适用于普通排行榜,但在使用leaderboardSetId时无法加载任何排行榜集.你可以直接链接排行榜集,如果是这样,你怎么做?

谢谢.

解决方法

请参考以下代码并交叉验证您的配置.

希望这会对你有所帮助.

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

}

更新

另请检查后端的排行榜配置.

这里有一些好帖子.

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

猜你在找的iOS相关文章