Yesterday I got Game Center notifications working for my achievements, but as soon as I tried them again today they didn't work? All I did was re-organise the code, and the achievements are going through to Game Center when you earn them. I cannot think of why my submitAchievements()
function could be wrong, but I'll paste it here:
GKAchievement.loadAchievements(completionHandler: { (achievements: [GKAchievement]?, error: Error?) in
let fifteenID = "15"
var fifteenAchievement: GKAchievement? = nil
// Find an existing achievement.
fifteenAchievement = achievements?.first(where: { $0.identifier == fifteenID})
// Otherwise, create a new achievement.
if fifteenAchievement == nil {
fifteenAchievement = GKAchievement(identifier: fifteenID)
}
if score >= 15 {
fifteenAchievement?.showsCompletionBanner = true
fifteenAchievement?.percentComplete = 100
}
// Insert code to report the percentage.
if error != nil {
// Handle the error that occurs.
print("Error: \(String(describing: error))")
}
let thirtyID = "30"
var thirtyAchievement: GKAchievement? = nil
// Find an existing achievement.
thirtyAchievement = achievements?.first(where: { $0.identifier == thirtyID})
// Otherwise, create a new achievement.
if thirtyAchievement == nil {
thirtyAchievement = GKAchievement(identifier: thirtyID)
}
if score >= 30 {
thirtyAchievement?.showsCompletionBanner = true
thirtyAchievement?.percentComplete = 100
}
// Insert code to report the percentage.
if error != nil {
// Handle the error that occurs.
print("Error: \(String(describing: error))")
}
let fiftyID = "50"
var fiftyAchievement: GKAchievement? = nil
// Find an existing achievement.
fiftyAchievement = achievements?.first(where: { $0.identifier == fiftyID})
// Otherwise, create a new achievement.
if fiftyAchievement == nil {
fiftyAchievement = GKAchievement(identifier: fiftyID)
}
if score >= 50 {
fiftyAchievement?.showsCompletionBanner = true
fiftyAchievement?.percentComplete = 100
}
// Insert code to report the percentage.
if error != nil {
// Handle the error that occurs.
print("Error: \(String(describing: error))")
}
let hundredID = "100"
var hundredAchievement: GKAchievement? = nil
// Find an existing achievement.
hundredAchievement = achievements?.first(where: { $0.identifier == hundredID})
// Otherwise, create a new achievement.
if hundredAchievement == nil {
hundredAchievement = GKAchievement(identifier: hundredID)
}
if score >= 100 {
hundredAchievement?.showsCompletionBanner = true
hundredAchievement?.percentComplete = 100
}
// Insert code to report the percentage.
if error != nil {
// Handle the error that occurs.
print("Error: \(String(describing: error))")
}
let negativeID = "1"
var negativeAchievement: GKAchievement? = nil
// Find an existing achievement.
negativeAchievement = achievements?.first(where: { $0.identifier == negativeID})
// Otherwise, create a new achievement.
if negativeAchievement == nil {
negativeAchievement = GKAchievement(identifier: negativeID)
}
if score < 0 {
negativeAchievement?.showsCompletionBanner = true
negativeAchievement?.percentComplete = 100
}
// Insert code to report the percentage.
if error != nil {
// Handle the error that occurs.
print("Error: \(String(describing: error))")
}
let clawID = "69"
var clawAchievement: GKAchievement? = nil
// Find an existing achievement.
clawAchievement = achievements?.first(where: { $0.identifier == clawID})
// Otherwise, create a new achievement.
if clawAchievement == nil {
clawAchievement = GKAchievement(identifier: clawID)
}
if claw {
clawAchievement?.showsCompletionBanner = true
clawAchievement?.percentComplete = 100
}
// Insert code to report the percentage.
if error != nil {
// Handle the error that occurs.
print("Error: \(String(describing: error))")
}
let achievementsToReport: [GKAchievement] = [fifteenAchievement!, thirtyAchievement!, fiftyAchievement!, hundredAchievement!, clawAchievement!, negativeAchievement!]
// Report the progress to Game Center.
GKAchievement.report(achievementsToReport, withCompletionHandler: {(error: Error?) in
if error != nil {
// Handle the error that occurs.
print("Error: \(String(describing: error))")
}
})
})
}
The showCompletionBanner
property for all the achievements are true, so I have no idea why they are showing up. Here is my git repository if you want to take a look:
I figured it out! After resetting the achievements, I had to close and reopen the app. Given that the final version won't have the reset functionality, this won't be an issue anymore.