I have been using GKLeaderboard.loadScoresWithCompletionHandler
and then the localPlayerScore
property so that I can retrieve the user's high score from Game Center
when my game is launched.
Now on iOS 7.0
to 8.1.2
, I get a valid GKScore
object when I access the localPlayerScore property. On iOS 8.1.3
however, I am getting nil which causes my app to crash. I get a run-time error saying it found nil while unwrapping an optional.
Here is a snippet of code relevant to the issue:
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")
}
}
}
}
I have been using this same code for my previous games (which are approved and live on the Appstore by the way) to retrieve the user's game center high score and have not faced this issue until 8.1.3
.
My current app was rejected by Apple because they tested it with an iOS 8.1.3
device and it crashed. I did some debugging and found out that this localPlayerScore
being nil is the root of the problem.
Anyone facing this issue lately? Does anyone know how to resolve this issue?
Any help is appreciated.
localPlayerScore
beingnil
, then it's not included in this excerpt. What's in the...
?