Saving game progress on iCloud: best solution? - ios

I'm working on a new iOS game and would like to save the progress online through iCloud. The saving is fairly frequent, but they are mostly doubles and ints with scores, unlocks and NSDate numbers.
Since internet will be required to play the game, I would like to save the game only online, not locally. When the app opens it should download the saved game from the server, and when it's done, launch the game.
Online currency will be sold through IAP, so I want to make sure the progress will not be lost even if the game is deleted and reinstalled, and of course, be available on multiple devices from the same user.
What's the best way to do this? Is it possible through iCloud? Key value seems like a mess because it doesn't sync quickly with iCloud. So Core Data?
I'm not looking for code to copy and paste, but I would like to create a discussion to find out the best solution for this case, if it's iCloud, setting up my own server, etc. Everywhere I look people say different things, but so far I couldn't find the best solution.
Thanks!

In iOS 8, you might want to look at GKSavedGame — it manages saved games associated with a Game Center player and syncs them through iCloud.
Otherwise, direct use of iCloud APIs sounds reasonable for your use case. If you write a small, well-defined set of values, the ubiquitous key-value store is very easy to use. If your save games are more complicated, write out a property list or encode your custom objects with NSKeyedArchiver, then use the NSFileManager APIs for syncing the resulting files through iCloud.
Going for Core Data or CloudKit is also possible, but sounds like it's more complicated than your game needs.

Related

Which is the best iCloud approach for my iOS notes app with text, photo, audio & drawing notes. Should I choose document storage or CloudKit?

I am developing an iOS notes app where a user can add image, text, audio and drawing notes. I want to implement iCloud synchronization between multiple devices. Out of the 3 options (key value, document storage, CloudKit) which one should I choose? I would like to implement the sharing of notes (collaboration) among users as well. I am using core data as my DB currently.
Key-value storage is out of question due do being too limited for your goal, document storage is only recommended when you need to handle and store the document as a whole. Since you are already using CoreData for local storage, it only makes sense to use regular CloudKit with it for cloud storage and sharing.
Synchronisation of CoreData and CloudKit can be tough. I am personally using a combo of RxCoreData and RxCloudKit libraries which provide some relief in synchronisation and some syntax sugar too.
A word in advance about uniqueness constraints: for CoreData, you define them based on key(s) or hash of all values, for CloudKit it is only possible (and also required) for the CKRecord key, to the best of my knowledge. So it is best to take care of it from the very start.
IMHO, CloudKit is the only opinion :)
I have a note app named marknote. And at the beginning I used iCloud document storage. It worked sometime, but not stable. The worst thing is, when and whether your documents can be synced is out of your control, instead it relies on Apple's daemon service. It becomes even worse when your documents are a little big, for instance several mega bytes.
So after fighting for some time, I changed to CloudKit. As #maxim-volgin has already pointed out, the implementation of CloudKit sync is tough, but it is reliable. And all headache just gone after switching to CloudKit.

What is the ideal way to store player statistics in a simple game

I’m looking to build a simple game as a personal project, and I was wondering what the preferred way to store player statistics is. In my professional line of work, I’ve worked with SQL databases, CoreData and so on. However, this simple game would really only need to store integers of the player’s stats. I’m wondering if NSUserDefaults is fine to use for this (simplest way, that way the game can act as an example even for beginners which is part of my purpose). I’m wondering however if using NSUserDefaults this way is looked down upon or not an ideal usage for it, and if there’s any drawback to it that I may not be aware of. The bonus to it is NSUserDefaults gives you iCloud backup for free, for the user. If anyone had any other suggestions, they are welcomed as well.

Game Data Persistence Across Devices

I'm still in the very early stages of a game, and I am saving information using a KeyChain wrapper class someone made. I wanted to ask for some advice early on, since I have time to change my approach.
My game has the potential to persist a fair amount of data about the player and what they've done, such as:
how much gold the player has
what items they've acquired (you can get about 50 items)
what skills, spells, and abilities they've chosen for their character
what experience level they are, max health, stats, etc
The reason I decided to store this in KeyChain was that I was told it's encrypted and much more difficult to tamper with. I felt there were other solutions such as the ones below, but I wrote some potential reasons why that might not be good:
Make everything web-based, and stored in a database somewhere on my server - I want my game to be playable offline
Use a local database (FMDB, let's say) - I could use a tool to edit the values directly, and give myself more health, etc.
Use Core Data - Never used this before, not sure if this is the same ease of tampering as #3?
GameCenter - Never used this before, so not sure what the lift is
NSPreferences - Preferences are more easily tampered with (i've used a tool to change the values pretty quickly)
So I am not sure if i'm completely wrong above, but let's say there is some degree of truth there and KeyChain is a good approach. The problem is now what if I want to then somehow allow the player to pick up a new device and pick up where they left off? How on earth would I serialize all that data I'm saving in keychain? I don't mind creating a giant JSON document of the values, and sending it along somewhere (where? to GameCenter?)
Any advice / pointers in the right direction would be good, especially now since i'm in the early stages and can make changes to step back.
Thanks so much everyone appreciate your time!
A few thoughts based on lessons learned (usually, "the hard way") that may (or may not) be helpful. :)
I see three requirements in your post: offline play (requiring local storage), data security (which is a massive topic in and of itself) and synchronization.
Playable offline:
Thus you need some sort of local storage. Keychain, Core Data, SQL, NSPreferences are all options. I don't know the limitations of the Keychain, so not really sure how suitable it is for continuous read/write of large chunks of data.
Data security:
They keychain protects your secrets when you're not logged in, and partitions them between apps. https://developer.apple.com/library/content/documentation/Security/Conceptual/keychainServConcepts/02concepts/concepts.html and http://evgenii.com/blog/sharing-keychain-in-ios/ give more details. That should prevent other tools from modifying other app's content on non-jailbroken devices.
Core Data, SQL, et al will be inside your app's container, which makes it harder for other tools to access on non-jailbroken devices. There's a good description here: https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
NSPreferences doesn't offer any security that I'm aware of. Plus, if they jailbreak the device, they basically have root access on a linux machine and can probably do anything they want.
In today's security world, the mantra is generally, "assume breach." As in, assume if they want in bad enough, they're going to find a way in. Thus you need to think about other layers of defense: partitioning your secrets so compromised secrets have limited value, encryption at rest and encryption in transit. Obfuscating the data before you write it/transmit is therefore another layer of defense (although they may still edit it and you'll have to handle potential garbage values).
You can chase security pretty far down the rabbit hole; you'll have to decide where the cost outweighs the benefit, and/or what risks you intend to defend against.
Synchronization across devices:
Thus you need a syncing solution. the iCloud Keychain syncs between devices, so if the keychain meets your storage needs, this probably will meet your sync needs, too. Again, I'm not sure if there are size or frequency-of-update constraints. I haven't used this, but this SO answer gives more info: https://stackoverflow.com/a/32606371/1641444. Based on Apple's docs (https://support.apple.com/en-us/HT204085) it looks like the user does have to enable syncing for this to work.
Otherwise, Apple provides GameCenter and CloudKit. Or you could explore 3rd party options. I've used GameCenter and CloudKit to sync data across devices. CloudKit wins over GameCenter, IMO, no contest.
With GameCenter, you gain multiplayer matchmaking and channels to share data between users. But, you have to adhere specifically to its structure which is IMO both extremely frustrating to work with and limited in functionality. Check the GameCenter and GKTurnBasedMatch tags here on SO for a taste of the problems.
CloudKit is a lower-level solution. It allows you to store a wide variety of data objects in iCloud and "subscribe" to change notifications. All data is contained within a container for the app, and has a "private" (user-specific) section and a public (shared by all users of the app) section. After watching the introductory WWDC videos on CloudKit, I was successfully sync'ing user settings between devices AND different users on different devices in no time. However, if you want some of the multiplayer features of GameCenter, you'll have to build the data model/subscriptions yourself. Since you support offline play, you'd need to save your data to a local storage solution and periodically upload it to iCloud for sync.
Conclusion (aka TL;DR)
So, my opinion is: none of the tools individually meet all three of your requirements, and you'll end up rolling your own solution for at least 1 req, regardless of which option you go for. In my multiplayer game, I'm trusting in Apple's filesystem (containers) to provide sufficient data security, I'm using a local storage option within the app's container, and I'm periodically writing NSData objects to cloudKit for synchronization across devices. I hit my frustration limit with GameCenter and pulled it from my app.
Star for a good question!!
I'm doing something similar with this. Right now, I'm using UserDefaults for primary storage. When I need to transfer data, I can save it to iCloudKit, or I could export it as a plist or json to use with AlamoFire, email, etc.
As for actually storing the data, I saw you mentioned CoreData.. that is kind of overkill! Look at NSCoder and NSKeyedArchiver.
Personally, I make my own save/load functions that manipulate dictionaries, then toss them into a UserDefaults key.
An example hierarchy of how I store dicts in UserDefaults (there are many ways to go about this)
Key:
Items->Equipment->Weapons->WeaponName
Value:
{ dictionary of data like AP, Cost, etc }
This to me is easier than using NSCoder, and certainly easier than CoreData!! The goal is to turn everything into a dictionary, which you can then easily put into UserDefaults, which allows you to easily create plists or save to the cloud.
With the above key system, you basically just do for loops to find what you need, parsing out each section.. so a function to display all equipment, you just load the entire UserDefaults.standard.dictionaryRepresentation, then search for keys that only have 'Items->Equipment' and so on.
Hope this helps!
I have some functions from my game I could share if you need more tips.
UPDATE:
If you are making this an online game, I would focus on learning the essentials of persistence and cloud usage first, then port it to a more secure platform that uses encryption.
Your best bet will be to create your own servers and transfer data that way. That will give you exactly what you want, when you want it, and with the security that you want.
If you want to use GameCenter, I would use GC as an in-between layer of something custom you created, so that way you can filter out the cheaters' scores / have more flexibility.

How should I store Cocoa app data?

I'm just getting started on making a board game using Cocoa/Objective-C. My plan is to first get it functional as a Mac OS X app, and then port it to iOS.
I saw something in the documentation for the Core Data API that made it sound like it was the recommended way to store data persistently, and made reference to the fact that iOS apps should be able to quit and be restored in exactly the same state. I got the initial impression that I should plan to use Core Data for any variable that has to do with the game's state to support this.
But as I'm learning more it seems like my initial impression isn't correct. Core Data seems more like something intended to provide similar features as embedded SQL, and is more complicated than is required just for storing the game state persistently on disk. Is there a simpler way to support fast app restoring in iOS apps, or is Core Data the way to go?
Core Data is fantastic for storing lots of data of different types, including custom objects.
However, if you're talking about storing things like high scores for a game or other simple int, float, BOOL, NSString, NSArray data, then for iOS NSUserDefaults is a quick and easy way to go.
Core Data lets you store data as objects, so if your game state can be described with native data types, Core Data just might work for you. You'd essentially be manipulating object states.
If you're looking for something a little more lightweight, look into the NSUserDefaults API (which is the same thing that the Settings App on iOS uses).
Alternatively, you could come up with your own format and use that, serializing your own data to disk.
I'd start with NSUserDefaults.

Is it safe to save experience and gold attributes (RPG-like) in a .plist-file?

I want to store earned xp and gold, so that my game loads the data whenever the game starts. I am using cocos2d on the iPhone, what would be a safe (doesn't need to be toooo safe) to store such data?
I don't want the user to be able to modify the xp-points too easily. Is that possible with .plists? Or do I have to use a Database like SQL lite? It would be very nice if the user couldn't just change the values of the .plist that easily.
It may be easiest to use NSUserPreferences.
How about storing the information in a dictionary, archiving it, then encrypting the data with one of the many available encryption categories like the one shared by Aquatic?
Nothing you can do would stop a determined and experienced hacker but this would make it difficult / effectively impossible for the average user.

Resources