Game Data Persistence Across Devices
Asked Answered
L

2

5

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:

  1. Make everything web-based, and stored in a database somewhere on my server - I want my game to be playable offline

  2. Use a local database (FMDB, let's say) - I could use a tool to edit the values directly, and give myself more health, etc.

  3. Use Core Data - Never used this before, not sure if this is the same ease of tampering as #3?
  4. GameCenter - Never used this before, so not sure what the lift is
  5. 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!

Luddite answered 17/12, 2016 at 3:33 Comment(0)
C
5

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://mcmap.net/q/169867/-icloud-sync-keychain. 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.

Commensurable answered 17/12, 2016 at 19:35 Comment(7)
Thank you so much for the answer; You've intrigued me with iCloud KeyChain. Is it related to keychain? If i'm able to produce a JSON document that represents the entire game (and what they player has acquired), can that be sent and retrieved to iCloud keychain? Thanks!Luddite
I don't really know, but I edited a couple of links into keychain syncing paragraph. One is a SO answer regarding how to set up syncing of Keychain objects. The other is user-documentation from Apple telling users how to enable it. That could be a major flaw, your game wouldn't work if they turn off keychain syncing on you. (it appears you just set a "please sync this item" attribute when you add it to the Keychain)Commensurable
Thanks! What do you think about the viability of this? splinter.com.au/2015/06/21/swift-keychain-wrapperLuddite
Looks promising. I haven't used it so I can't vouch for it, but it looks like a reasonable wrapper for managing Keychain items.Commensurable
It seems like GKSavedGame developer.apple.com/reference/gamekit/gksavedgame is a modern way to go that uses apple cloud in the end! Will share an answer when I research more..Luddite
When I mentioned GameCenter, i was referring to all classes that begin with GK. I probably should have said "gamekit". From what i experienced, the GK classes are all a bumpy rideCommensurable
Let us continue this discussion in chat.Luddite
G
2

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.

Guarnerius answered 17/12, 2016 at 19:8 Comment(3)
I strongly concur with your recommendation of cloudkit for synchronization. I've found it to be very effective and easy to use.Commensurable
Thanks for taking the time to respond! :) Would you say that, given I can create a large JSON document from all my keychain values (I have code to do just that) and produce a representation of the user's state of the game, would you say that I simply send/retrieve that via CloudKit?Luddite
@Luddite did you still need help with this? No answer was accepted yet :)Guarnerius

© 2022 - 2024 — McMap. All rights reserved.