如何在 iOS 中显示排行榜集
Posted
技术标签:
【中文标题】如何在 iOS 中显示排行榜集【英文标题】:How to display Leaderboard Sets in iOS 【发布时间】:2017-05-05 08:21:52 【问题描述】:我昨晚试图让它工作,但它不会。有人可以帮忙吗?
我使用以下代码显示排行榜:
gameCenterViewController.leaderboardIdentifier = leaderboardId
这适用于普通排行榜,但在我使用 leaderboardSetId 时无法加载任何排行榜集。您可以直接链接到排行榜集吗?如果可以,您如何做到这一点?
谢谢。
【问题讨论】:
【参考方案1】:请参考以下代码并交叉验证您的配置。
希望这会对你有所帮助。
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)
更新
另外,请务必从后端检查您的排行榜配置。
这里有一些好帖子。
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
【讨论】:
我有正常的排行榜显示它只是不显示的排行榜集。我尝试用我的代码中的排行榜集 ID 替换 leaderboardIds,但这没有用。我认为此解决方案假定您只需要这样做? 是的,这是最低要求,您可以尝试整合我在这里给出的代码并检查。【参考方案2】:确保您已经验证了本地播放器和在 iTunes Connect 中设置的排行榜,您需要report your scores to GameCenter 确保您使用 64 位 Int。 现在您可以添加它来显示本地玩家的排行榜:
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)
【讨论】:
以上是关于如何在 iOS 中显示排行榜集的主要内容,如果未能解决你的问题,请参考以下文章
如何更改 Game Center (iOS) 中的排行榜图标?
iOS Google Play Games SDK - 如何邀请好友加入社交圈?