I am creating a fairly simple game in Swift and I am unsure of which data persistence method would be best suited for the project. This is my first "real" project so please excuse my ignorance.
I have to be able to store the following data for users:
- On/Off states for game sound
- The users' high-score
- A game currency unit that users may purchase In-App
- An indicator of whether Ads are enabled or disabled in the game
Also the user has to be able to restore all data from iCloud (I know that's a whole separate issue). Can I accomplish this with just NSUserDefaults/plists or is this more up the alley of Core Data?
NSUserDefaults is great for storing very small amounts of data. In your case this may be a good choice.
There is NSUserDefaults. Yes you can save those small settings.
There is also iOS keychain to store the sensitive data like amount of currency but I don't think it is that important to keep it in the keychain.
If you are interested in the keychain check out this repository.
Locksmith
Finally if you try to save the settings in the iCloud you can use the iCloud key value storage.
iCloud Preference storage
Related
I'm new to Swift & Firebase and I haven't really figured it out yet. Currently I'm thinking about adding a Database to store data for the user to fetch in case he switches device.
So far I've stored data with UserDefaults. Now that I'll probably be using a Database, do I still need UserDefaults? What if the user is not able to get internet connection and closes the app, before he gets to synchronize his data going online - will there be a danger of data loss?
Or is it best to keep both - UserDefaults and the Database?
A UserDefaults from Apple documentation:
An interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app. https://developer.apple.com/documentation/foundation/userdefaults
UserDefaults is a great solution for keeping unique values between app sessions. For example user settings, preferences (Simple, dummy data).
To share data between 2 devices you can use Firebase Storage. Another option is a CloudKit.
Synchronisation - this is more about what you need and how you would like to handle it. You can base on online data, so there would not be need to synchronise (you are always up to date). Or make periodically synchronisation, but you would need to think about best solution for local database.
It depends on what you are doing. Using user defaults is certainly fine for holding defaults but not information. What if 2 people use the same device, then you have user defaults for one user and a database for the other. I personally would use just one. If you are learning Firebase I would suggest 2 tutorials:
https://www.youtube.com/watch?v=F42CzqIBxHQ&t=406s
https://www.youtube.com/watch?v=RMudKhNY0sI&t=627s
and the question I just had answered about reading and writing to a secure database: User already authenticated in app and Firebase realtime database returns failed: permission_denied
From Apple's NSUbiquitousKeyValueStore documentation:
If you write to the key-value store object when the user is not signed into an iCloud account, the data is stored locally until the next synchronization opportunity. When the user signs into an iCloud account, the system automatically reconciles your local, on-disk keys and values with those on the iCloud server.
Therefore if a user never signs into an iCloud account, the key-value store object is stored locally indefinitely, much like NSUserDefaults.
In this case, should we all stop using NSUserDefaults and just use NSUbiquitousKeyValueStore as a 'default' for all apps? What are the disadvantages of this approach?
An advantage I can see is that from a user perspective the app preferences will be synced across all their devices, which is most likely a better user experience!
We should clearly understand that NSUbiquitousKeyValueStore in the iCloud is for the configuration and tracking of the app state across all devices of the certain account.
Some facts bout NSUbiquitousKeyValueStore.
In the documentation we can find that :
Avoid using this class for data that is essential to your app’s
behavior when offline; instead, store such data directly into the
local user defaults database.
Also size of the data that is possible to save is relatively small.
The total amount of space available in your app’s key-value store, for
a given user, is 1 MB. There is a per-key value size limit of 1 MB,
and a maximum of 1024 keys.
I have found if the user goes into their iCloud settings and turns your app switch off - you will no longer be able to save to NSUbiquitousKeyValueStore.
Your app will start with it enabled. Then you may save data there. Once iCloud is turned off, it will always be accessible to read - but if you update the data and restart the app, i believe you will see it revert back to what it was before the user turned off iCloud support in iCloud settings.
This is a problem I am working with at the moment. I am trying to find a way to test if that switch is on/off and change my data store to NSUserDefaults.
Correct me if I am wrong but that is what I have discovered so far.
-mark
edit: this is only if you have registered for iCloud Documents. If you are ONLY using NSUbiquitousKeyValueStore it should be virtually the same - I believe.
edit-edit: now its acting different. The switch is there when ONLY NSUbiquitousKeyValueStore is checked in the iCloud capabilities. So looking - again - for a way to detect if that switch is flipped for an app in iCloud Settings.
I am building some quiz with consumable coins to use. I used NSUserDefault to save coins on device and its working. I am also using CloudKit for data in qiuz.
No trouble is how to restore coins if user switch device? There is a part solution with keychain but it only works on same device.
Is there way to store coins via dashboard in cloudkit with my data?
thank in advance
Try to use key-value storage in iCloud. It works almost the same way as NSUserDefaults, so you can switch totally to 'key-value storage' instead of NSUserDefaults.
"Designing for Key-Value Data in iCloud" by Apple
As well as NSUserDefaults 'key-value storage' allow developers to save simple data types, yet saved data is available across user devices with enabled iCloud.
First step: enable 'key-value storage' in application's Capabilities settings
Second step: on application didFinishLaunchingWithOptions: implement subscription to NSUbiquitousKeyValueStoreDidChangeExternallyNotification notification at NSNotificationCenter.
Third step: write and read data to NSUbiquitousKeyValueStore.defaultStore() the same way you do it with NSUserDefaults.
Consider a way to resolve Key-Value Conflicts (in case user simaltaneously uses two or more devices, or one of the devices hadn't internet connection, etc)
Also, there is a limit on amount of data saved in Key-Value storage, number of keys, length of keys' names... Check the above link to Apple's Guide.
I an building my first iOS application and I need to store the user registration details of the user using the application. The details include his mobile number and a unique id( uuid ) which I use to contact with the backend. It would be great if I could get a suggestion on where to store this user details.
Should I be storing this in the NSUserDefaults or should I be using Keychains to store this data or even may be a using a user model in the database ( I would need a database in any case to store a few other details ). Just to add on, I also would like to perform a few validations like if the mobile number is of proper format and so on before I could actually save it. Also can any one please suggest on the security aspects of different storage mechanisms possible here?
Any help on this would be much appreciated.
The most secure way would be to use keychain services as the data is encrypted but in your scenario it seems a bit over kill. I would recommend either just using NSUserDefaults or an sqlite database I wouldn't really recommend storing in a plist as this can be accessed really easily.
But this all depends on the data you are getting, if it was just uuid and mobile number then NSUserDefaults would do probably, whereas if you were getting usernames and passwords and other personal data I would looking a mix of keychain and sqlite database.
Also you could use coredata file to store user data but seems a bit over kill as well for for such little data.
Just a little note you are actually not allowed to get the iPhones mobile phone number programmatically, getting this would use Private APIs that Apple would reject your app for using.
2.5 Apps that use non-public APIs will be rejected
So you would have to ask he user for this.
Database selection is totally depend on the architecture and security, if you just need to store the few information like login details and some field then Keychain for login details and plist for data is best option, but if your application also working with services and fetching and saving lots of data and continuously updating it then a serious database structure required. In that scenario core data and sqlite both are good option depends on your preference
Following ways you can save details.
In NSUserDefaults
In coredata file.
In sqlite database
Plist file.(Not recommended)
You can save data at server site using webservice.
Any one of these you can use according to your requirement and data.
Cheers :)
If you store information on the UserDefaults, a jailbroken device can see the information you have stored, it is a plist after all. If you are going to keep sensitive data on your device, user defaults itself is not a good option. Possible alternatives:
Use keychain: Keychain is a tool to keep usernames & passwords securely on a device; so you may need to find a way to convert all the info you have mentioned ( a dict, I presume? ) into NSData and put into/get from the keychain but it's been explained on other threads. Additionally, keep in mind that when the app is deleted, keychain data will persist on the device.
UserDefaults & encrpytion: If you can encrypt the data yourself, than using UserDefaults might be a better option. Its more straightforward than keychain and it will be deleted if you delete the app from the device (which may be the thing you want, or not. It depends)
I'm making an app for iOS devices. It's not my first app but it doesn't important. My app use NSUserDefaults to store data which are using during application runtime. All works good while user wants to delete app. After this and after installing/compiling app again on same device all settings from NSUserDefaults are removed - It's look like app is first time on this device.
The question are:
How should I store data if I want to read it after removing and installing again an app?
If not NSUserDefaults then where to store this data?
Thank you in advance.
the nsuserdefaults are not preserved. nothing in your app's sandbox is BUT for the keychain. values in the keychain are not erased.
so use the keychain for values that you want to keep.
BUT don't store everything in there. normally when a user deletes an app he wants stuff gone.
the only other way I can see would be your own server where users can store backups, the dropbbox api or icloud
I think you could store defaults in your custom plist, and then use iCloud, which is built in iOS. iCloud will automatically backup documents from your app, and then you have them available on new installations since apple handles this.
Have a look at apple's official documentation about storing key value data on iCloud:
https://developer.apple.com/library/ios/#documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForKey-ValueDataIniCloud.html#//apple_ref/doc/uid/TP40012094-CH7-SW1
In that link they explain how to add key value data on iCloud to your app.
You want to use the keychain, docs here.
This is the only way on the device. Possibly you could use iCloud depending on exactly what you're trying to achieve.