this is how I solved
func loadAchievementPercentages() {
print("get % ach")
GKAchievement.loadAchievementsWithCompletionHandler { (allAchievements, error) -> Void in
if error != nil {
print("GC could not load ach, error:\(error)")
}
else
{
//nil if no progress on any achiement
if(allAchievements != nil) {
for theAchiement in allAchievements! {
if let singleAchievement:GKAchievement = theAchiement {
self.gameCenterAchievements[singleAchievement.identifier!] = singleAchievement
}
}
}
for(id,achievement) in self.gameCenterAchievements {
print("\(id) - \(achievement.percentComplete)")
}
}
}
}
func incrementCurrentPercentageOfAchievement (identifier:String, amount:Double) {
if GKLocalPlayer.localPlayer().authenticated {
var currentPercentFound:Bool = false
if ( gameCenterAchievements.count != 0) {
for (id,achievement) in gameCenterAchievements {
if (id == identifier) {
//progress on the achievement found
currentPercentFound = true
var currentPercent:Double = achievement.percentComplete
currentPercent = currentPercent + amount
reportAchievement(identifier,percentComplete:currentPercent)
break
}
}
}
if (currentPercentFound == false) {
//no progress on the achievement
reportAchievement(identifier,percentComplete:amount)
}
}
}
func reportAchievement (identifier:String, percentComplete:Double) {
let achievement = GKAchievement(identifier: identifier)
achievement.percentComplete = percentComplete
let achievementArray: [GKAchievement] = [achievement]
GKAchievement.reportAchievements(achievementArray, withCompletionHandler: {
error -> Void in
if ( error != nil) {
print(error)
}
else {
print ("reported achievement with % complete of \(percentComplete)")
self.gameCenterAchievements.removeAll()
self.loadAchievementPercentages()
}
})
}
in the view did load I have this
if (conditions for the achievement)
{
incrementCurrentPercentageOfAchievement("achievementID", amount: 100)
}
Tested now and it worked,now i want to do something different,I don't want to increment an achievement percentage but to set the percentage, for example:
Achievement - combo of 10
- Play a game
- Combo of 8 and shows 80% progress on the achievement
- Another game
- Combo of 2
With the code I have now it should unlock the achievement because the percentage is cumulative, so I have to write another function to change the achievement progress percentage and to set it to 20
Now if in a game I met the requirements and unlock the achievement,and in the next game I don't get this condition again, the achievement is still unlocked, right?