我一直在使用GKLeaderboard.loadScoresWithCompletionHandler
然后属性,以便我可以从我的游戏启动时localPlayerScore
检索用户的高分。Game Center
现在在 iOS7.0
上,当我访问 localPlayerScore 属性时8.1.2
,我得到了一个有效的对象。GKScore
然而,在 iOS 上8.1.3
,我得到了 nil,这导致我的应用程序崩溃。我收到一个运行时错误,说它在展开可选时发现 nil。
这是与该问题相关的代码片段:
func compareLocalHighScoreFromLeaderboards()
{
// This is to fetch data from the high score leaderboard
let leaderboardRequest = GKLeaderboard()
leaderboardRequest.identifier = GlobalVariables.sharedInstance._highScoreLeaderboardID
leaderboardRequest.loadScoresWithCompletionHandler { (scores, error) -> Void in
if error != nil
{
println("Error fetching score from leaderboards: \(error))")
}
else if scores != nil
{
println("entered loading scores from leaderboards")
let leaderboardScore = leaderboardRequest.localPlayerScore // this returns a GKScore object
var playerLocalHighScore = NSUserDefaults.standardUserDefaults().objectForKey(GlobalVariables.sharedInstance._highScoreKey) as NSNumber
// Check first if the saved highscore is updated.
if playerLocalHighScore.longLongValue != leaderboardScore.value
{
// this means that we don't have the updated leaderboard score in our device
if playerLocalHighScore.longLongValue > leaderboardScore.value
{
println("Local score is greater than leaderboard highscore. Do nothing because GameKit will automatically report to GC when there is internet connectivity")
}
else if playerLocalHighScore.longLongValue < leaderboardScore.value
{
// update the local highscore with the leaderboard highscore
let updatedHighscore: NSNumber = NSNumber(longLong: leaderboardScore.value)
NSUserDefaults.standardUserDefaults().setObject(updatedHighscore, forKey: GlobalVariables.sharedInstance._highScoreKey)
// send notification message to MainMenuScene to update the SKLabelNode
NSNotificationCenter.defaultCenter().postNotificationName(UpdatedLocalHighScore, object: nil)
}
}
else
{
println("The local highscore and the leaderboard highscore are already in sync")
}
}
}
}
我一直在为我以前的游戏使用相同的代码(顺便说一下,这些游戏已获得批准并在 Appstore 上运行)来检索用户的游戏中心高分,直到8.1.3
.
我当前的应用程序被 Apple 拒绝了,因为他们使用 iOS8.1.3
设备对其进行了测试并且它崩溃了。我做了一些调试,发现这localPlayerScore
是 nil 是问题的根源。
最近有人遇到这个问题吗?有谁知道如何解决这个问题?
任何帮助表示赞赏。