Let's say I had a game where in the beginning the user gets to choose a couple of things like name, gender, etc, and that will affect a lot of aspects in the game such as the sprite used to render the user, and the text that is displayed.
So every time I leave one area of the game and enter a new one I should figure out according to the user data - what sprite to use and what text to prep. Should I make a separate class with a dictionary just for the user's data and then just call it whenever I enter a new area?
Thanks yall.
There is a few ways to do this.
I prefer to use a singleton GameData class with NSCoding to house all the properties (arrays, dictionaries etc) that need to be saved permanently.
I like it this way because it makes code more readable and also has the advantage that you can access the properties from anywhere in your project. This way it's also easy to include icloud key value storage.
You can read these 2 questions I answered for a simple example of how this can look
SpriteKit: Why does it wait one round for the score to update? (Swift)
Is there a better way to save a custom class to NSUserDefaults than encoding and decoding everything with NSCoder?
There is a also few good tutorials around such as this
https://www.hackingwithswift.com/read/12/3/fixing-project-10-nscoding
To securely save data you can/should use keychain. To read more about it check out this question.
How secure is NSUserDefaults on iOS 8,9?
Related
My app starts with five cells (Ca, Alk, Mg, PO3, NO3). When the user taps on the cell they want to dose lets say Ca, it segues to a screen that have all of the Ca products. From there they get to pick which Ca product they need to use, which segues to the calculator screen where they can calculate the dosage they need.
I need to find a way for the user to save the product they chose, so lets say they selected Ca, I want them to save the product in the Ca list, if they chose Alk, I want them to save the product in the Alk list.
I am lost here whether UserDefaults is okay for this or if I need CoreData.
I personally would use CoreData over UserDefaults for this. CoreData is designed to solve persistence in a more general way that will scale with the needs of what you are building. It's possible to do what you describe with either one - but the modeling of your data, with generated files will provide your code with safety, migrations, and performance if you use core data.
See Here for determining which data storage types to use based on the problem you are trying to solve.
For help in how to actually use CoreData - this article gives some code examples for basic CRUD operations.
Basic CoreData Tutorial
I am developing a game with Spritekit and I'm now at a point where I have to keep track of the user's highscore. There are various different methods for storage with the easiest one being NSUserDefaults, but it's recommend for preferences.
However, I found this Keychain wrapper which tends to save a string with a key:
https://github.com/jrendel/SwiftKeychainWrapper
Do you think it's good to use it? My idea is to convert the score (Integer) to a String before saving it and while retrieving the highscore, I will convert it back from String to Int. Do you think it's a good idea?
The main advantages of the key chain I can see are that entries persists when an app is deleted and can't easily be changed. UserDefaults would be tied to your app Id and lost on app delete: unless you used the global domain.
You could look to use CloudKit key/value storage to achieve cross device high scores, storing the data in the cloud. This would be more work and you would need to add conflict resolution.
Depending on how serious you are taking high scores, you should perhaps consider integrating with Game Center? Ideal if you want high score etc to persist across more than one device.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html
Im working on a multiplayer turn based game in xcode for iphone/ipad im considering my options when it comes to where to save users details such as name / points ect. I was considering creating a plist and storing data in this also i have considered nsuserdefaults and also core data.
Basically majority of the time the user will be adding new details on every launch however this will not be the case 100% of the time.
What would you guys consider the best approach?
This really depends on multiple factors:
How much data is it going to be saved?
How fast the data needs to be loaded?
I suggest you create a serializable object, that conforms to NSCoding protocol. There is also an option to save this as JSON or like you noted, plist.
I think Core Data is too much boiler plate, if this is all you are storing. NSUserDefaults is fast enough and already prepared for you to use.
I don't think someone can give you the correct answer, I suggest you experiment with all the options and see which one is the best for you.
I have an IOS app with multiple UIIMageView objects and wondering what would be the best way to remember their locations if the user killed the app or if they wanted to save that particular layout for future use.
I'm a beginner at this so looking for the simplest and most effective method. The only thing I can think of at the moment is to extract CGPoint location and tag of each object and save to NSUserDefaults or something but i'm hoping theres an easier way...
For something as simple as storing CGPoints, I'd recommend using NSUserDefaults, because you can very easy manage the data in it using a NSMutableArray.
You can go for Core Data and then maybe integrate iCloud, however NSUserDefaults keeps it simple.
Since your problem is to do with the state of the app, I'd recommend reading what Apple has to say about it.
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.