Tomasz Szulc
Tomasz Szulc

Reputation: 4235

How to store user-data correctly on iOS? Case with NSUserDefaults

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:

  1. How should I store data if I want to read it after removing and installing again an app?
  2. If not NSUserDefaults then where to store this data?

Thank you in advance.

Upvotes: 0

Views: 685

Answers (3)

The dude
The dude

Reputation: 7924

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.

Upvotes: 1

Wain
Wain

Reputation: 119031

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.

Upvotes: 1

Daij-Djan
Daij-Djan

Reputation: 50099

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

Upvotes: 2

Related Questions