26

I'm aware of NSUserDefaults for saving/restoring user preferences. What is the equivalent class for an application? For example, the application may have a "last run" field; or it may have a field for a unique identification of the device for use at the application level.

My intention is to keep the application's settings (not user's settings) out of the Settings Application, and not backup those settings in iTunes, Time Machine, {whatever}.

I'm getting a lot of noise for Java and C#, but not much for iOS/iPhone/iPad.

jww
  • 97,681
  • 90
  • 411
  • 885
  • The iPhone is a One User operating system. You can not log in with another username. So why would you need application settings, different from user settings ? Or am I missing something ? – bartvdpoel Jul 08 '11 at 11:27

3 Answers3

70

NSUserDefaults can be used for what you're asking.

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"shownPrompt"]) {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"shownPrompt"];
    // Show your prompt or whatever
}

That's a working code snippet. If the key is false, it sets it to true and shows the prompt. The next time this code runs, the key will already by true, so the prompt won't be shown.

NSUserDefaults is specific to the current app on the current device, and is similar to an NSMutableDictionary in that it's a key-value system, with the difference that instead of instantiating your own, there's a universal shared instance for your whole app, that doesn't get erased when the app exits.

NSUserDefaults is perfect for saving things like whether something has been shown, the date of last run, etc. Read the docs here: https://developer.apple.com/documentation/foundation/userdefaults

Don't be put off by the 'user preferences' part. You can use it to save anything you want (as long as it is or can be converted to an NSObject which implements <NSCoding>, which basically means NSString, NSDictionary, NSArray, NSNumber, UITextField, int, float, bool, etc.).

Just to clarify, stuff you put in NSUserDefaults will not, under any circumstances, automagically turn up in the Settings app. It will be kept completely private and hidden. For something to appear in Settings, you need to add a Settings bundle to your app, and manually add keys to it for each and every value that you want to be visible in the Settings app.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Greg
  • 9,068
  • 6
  • 49
  • 91
  • 2
    NightLeopard - I would have liked to given you credit for the answer also, but I could not. The best I could do is a SO thumbs up. – jww Jul 08 '11 at 12:15
  • 1
    Make sure you call `[standardUserDefaults synchronize]` at some point to commit the changes. – bendytree May 18 '13 at 01:31
  • 1
    @bendytree According to Apple's documentation: Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes. – Tom Jul 22 '13 at 14:51
  • This should be the accepted answer. The OP seems to misunderstand that UserDefaults (private, app-internal) != Settings bundle (public, user-facing). – MandisaW Apr 05 '18 at 15:35
  • Thanks for the detailed explanation – frezq Aug 20 '18 at 20:53
11

if you can store value by NSUserDefaults, then it is good to store application preferences too.

or add settings.plist on your project and read that (what you are not changing later)

and you can use like.,

+ (NSDictionary*)getBundlePlist:(NSString *)plistName
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves           
                                          format:&format errorDescription:&errorDesc];
    return temp;
}

+ (id) getPropValue:(NSString *)PropertyName
{   // I am supposing you had add your app preferences on settings.plist.
    return [[Property getBundlePlist:@"settings"] objectForKey:PropertyName];
    //here Property is my class name, then you can use value by 
    //NSString *value = [Property getPropValue:@"setting1"];
}
Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61
  • Or you could put everything into an array or dictionary and call `writeToFile:` – Greg Jul 08 '11 at 12:10
  • that is good, as I understand your solution, which will save after app execution. hmm I was telling him to add values on plist file in IDE. – Mujah Maskey Jul 08 '11 at 12:13
0

It's hard to tell what you're asking. It sounds like NSUserDefaults is what your looking for, but you claim that you're already aware of it. So what's your problem?

Erik B
  • 40,889
  • 25
  • 119
  • 135
  • On the device, some items (such as 'last run') should not show up in the Settings Application, and should not be backed up. Its my intention to keep them private. – jww Jul 08 '11 at 12:01
  • 7
    @noloader when you put something in NSUserDefaults, it does **NOT** automatically show up in the Settings. To make something show up in the settings, you have to specifically add a Settings bundle to your app and _specifically_ add a key to it that will set that value. Nothing will automagically appear. I use `NSUserDefaults` all the time and my app doesn't even _have_ a Settings bundle, nor does it show up in Settings. – Greg Jul 08 '11 at 12:13