iOS 8.1.3上的GKLeaderboard.localPlayerScore返回nil?

时间:2015-02-14 19:41:51

标签: ios swift sprite-kit game-center game-center-leaderboard

我一直在使用GKLeaderboard.loadScoresWithCompletionHandler,然后使用localPlayerScore属性,以便在我的游戏启动时可以从Game Center检索用户的高分。

现在,在iOS 7.08.1.2,我访问localPlayerScore属性时会收到有效的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上使用),以检索用户的游戏中心高分并且在{{1}之前未遇到此问题}。

我当前的应用被Apple拒绝,因为他们使用iOS 8.1.3设备对其进行了测试,然后崩溃了。我做了一些调试,发现这个8.1.3是nil是问题的根源。

最近有人面对这个问题吗?有谁知道如何解决这个问题?

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

更改此else if scores != nil的{​​{1}},因为else if leaderBoardRequest.localPlayerScore != nil包含scores的所有信息以及用户第一次打开游戏时,leaderboardRequest.identifier是空,但leaderBoardRequest.localPlayerScore不一定

scores