如何从swift(iOS)中的特定游戏中心排行榜中获取前10个分数?
我想从领导者那里获得前10名得分和球员,并在比赛中建立一个自定义的“HALL OF FAMES”。 如何从leaderbaord获取数据?
答案 0 :(得分:6)
同时我找到了答案:
let leaderBoardRequest = GKLeaderboard()
leaderBoardRequest.identifier = kGcIdHighScore // my GC Leaderboard ID
leaderBoardRequest.playerScope = GKLeaderboardPlayerScope.Global
leaderBoardRequest.timeScope = GKLeaderboardTimeScope.AllTime
leaderBoardRequest.range = NSMakeRange(1,10) // top 10
leaderBoardRequest.loadScoresWithCompletionHandler
{ (scores, error) -> Void in
if (error != nil)
{
print("Error: \(error!.localizedDescription)")
} else if (scores != nil)
{
print("got top \(scores?.count) scores" )
var ii = 0
while (ii < scores?.count)
{
NSLog("%i ....... %@ %i %i", Int(ii+1), scores![ii].player.alias! , scores![ii].rank , scores![ii].value )
ii = ii + 1
}
}
} // end leaderBoardRequest.loadScoresWithCompletionHandler
答案 1 :(得分:1)
我已经使用了上面的内容并将其调整为与Swift 4一起使用 我已经创建了一个函数来获取其中的分数我将结果附加到元组数组中,其中: 0元素是实现的地方 1元素是GameCenter玩家名称 2元素是得分
我用它将所有内容填充到tableView中。根据您的需要调整它
import GameKit
import UIKit
class HighScoresViewController: UIViewController, GKGameCenterControllerDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var scoreTableView: UITableView!
var highScores = [(String,String,String)]()
let LEADERBOARD_ID = "com.yourLeaderboard"
override func viewDidLoad() {
super.viewDidLoad()
scoreTableView.delegate = self
scoreTableView.dataSource = self
getScores()
}
func getScores(){
let leaderBoardRequest = GKLeaderboard()
leaderBoardRequest.identifier = LEADERBOARD_ID // my GC Leaderboard ID
leaderBoardRequest.playerScope = GKLeaderboardPlayerScope.global
leaderBoardRequest.timeScope = GKLeaderboardTimeScope.allTime
leaderBoardRequest.range = NSMakeRange(1,10) // top 10
leaderBoardRequest.loadScores
{ (scores, error) -> Void in
if (error != nil)
{
print("Error: \(error!.localizedDescription)")
} else if (scores != nil)
{
print("Top \(scores?.count ?? 0) scores" )
var ii = 0
while (ii < scores?.count ?? 0)
{
self.highScores.append(("\(scores![ii].rank)" , "\(scores![ii].player?.alias! ?? "Player")" , "\(scores![ii].value)"))
print(Int(ii+1) , scores![ii].rank , scores![ii].player?.alias! ?? "Player" , scores![ii].value )
ii = ii + 1
}
self.refreshTableData() }
} // end leaderBoardRequest.loadScoresWithCompletionHandler
}
func refreshTableData(){
print("refreshed")
self.scoreTableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "scoreCell", for: indexPath) as! ScoreTableViewCell
cell.placeAchieved.text = highScores[indexPath.row].0
cell.playerName.text = highScores[indexPath.row].1
cell.playerScore.text = highScores[indexPath.row].2
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return highScores.count
}
}
这是我的TableViewCell文件,包含标签
import UIKit
class ScoreTableViewCell: UITableViewCell {
@IBOutlet weak var placeAchieved: UILabel!
@IBOutlet weak var playerName: UILabel!
@IBOutlet weak var playerScore: UILabel!
}