R – Ist it a good idea to use NSUserDefaults to store the latest state of an iPhone application

cocoa-touchiphonensuserdefaultsuikit

I have an view-based application where the user can do a lot of customization things, like selecting colors, selecting pictures, and so on.

First, I thought about using sqlite3 for that, but since this would result in a table with only one row (no multi-user app), it seems like a big overhead to me. Then I heard about NSUserDefaults. But I am not sure where this data is stored. Is it stored in the app's sandbox? Or is it stored somewhere else? Do other apps have access to that data? Is it good for remembering this kind of customization stuff?

Best Answer

The NSUserDefaults class is meant for storing user preferences for your application. It's stored in a plist file in the application's sandbox Library directory.

[[NSUserDefaults standardUserDefaults] setObject:@"Test" forKey:@"myPref"];
NSString *val = [[NSUserDefaults standardUserDefaults] stringforKey:@"myPref"];