Friday, February 10, 2012

iCloud Incrementing Only

For our game Pawn'd, we wanted to have the high scores transfer between all your devices using iCloud.  But we didn't want it to mess up, overwriting high scores with a lower value set on another device.

Here is MBCloudNumber.  It's a class to use for writing and reading number values (integer for now, but you could add floats easily.)



// Initialize this in your -applicationDidFinishLaunching
// so iCloud changes are detected and initial values are copied over.
[[MBCloudNumber globalSettings] synchronize];


//To set a value, just pass it in, saying whether it should only increase/decrease 
[[MBCloudNumber globalSettings] setNumber:score forKey:@"HighScore" increases:YES];

//And then it's easy to read values out too. 
NSInteger  highScore = [[MBCloudNumber globalSettings] numberForKey:@"HighScore"];


You'll still need to setup the iCloud Key-Value Store entitlements in your project.

No comments: