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.
Related
I have an application that is implementing storage using Key/Value pairs in iCloud. From what I read in the documentation this is almost identical to the way NSUserDefaults work.
However this potentially creates a problem because the user should not have the ability to tamper with the app data stored in there. Does this mean that the user can access this data and modify it? Or is it private to the application?
Okay reading deeply in the documentation it says
If your app needs to store passwords, do not use iCloud storage APIs
for that. The correct API for storing and managing passwords is
Keychain Services, as described in Keychain Services Reference.
I found this text here just one line before the last table :)
I also found somewhere that the user can delete his iCloud data manually which can be counted as a modification.
Also, read here, section fro "Start Fresh If Your iCloud Data Becomes Inconsistent During Development" where it says how you can clean the container. Maybe you can check what is visible inside.
It depends what type of data you are storing in the iCloud if it's sensitive then I would use keychain services approach and avoid storing sensitive information on the iCloud.
From the question it seems like you are storing the data in key-value pairs, usually, it's recommended to store preferences, settings, and simple app state and that should be ok because the user can change those, you should choose the right iCloud API for what you want to store
With iCloud the user can always delete the information it has stored as mentioned in the documentation
There may be times when a user wants to delete content from iCloud.
Provide UI to help your users understand that deleting a document from
iCloud removes it from the user’s iCloud account and from all of their
iCloud-enabled devices. Provide users with the opportunity to confirm
or cancel deletion
When you ask
Or is it private to the application?
There's an iCloud identifier in your entitlements file. If it's the same in both apps you'll be able to access the same data/documents across both the apps.
Hope that helps.
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 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
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.