Storing iPhone application settings in app
Asked Answered
A

3

21

My iPhone app has few settings that users is likely to change quite often. I would like to know if there's any suggested way of handling such settings (reading and saving them). On Apple sites I found only a tutorial about integrating your application settings with Settings app (link) but I don't want a user to exit my app so he could just change the option.

Is there any default mechanism to handle such settings in app itself or do I have to implement a solution of my own?

Aleksandropol answered 9/2, 2010 at 10:28 Comment(0)
C
42

Best and easiest way to store settings in the iPhone is through NSUserDefaults. Keeps you from having to deal with the file system or plists or any of that other stuff.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *storedVal = @"This is what you want to save";
NSString *key = @"storedVal"; // the key for the data

[defaults setObject:storedVal forKey:key];
[defaults synchronize]; // this method is optional


// Get the results out
NSString *results = [defaults stringForKey:key];

Here's what Apple says on the types of objects you can store in the Defaults

A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

There are some more caveats, like if you store an NSDictionary the key values must be strings.

Ceraceous answered 9/2, 2010 at 10:42 Comment(4)
Is it possible to store values of types other then NSString like numbers or dates?Aleksandropol
Thanks mate, that's the kind of answer I wanted to hear.Aleksandropol
Is there an advantage to doing this over just using a framework like "www.inappsettingskit.com" that Ortwin posted about?Coefficient
@Coefficient inappsettingskit is great if that's what you want. This stuff is easy to implement, though. If you just have a few things you want to save, I'd say writing all the code yourself would be easier than integrating the settings kit into your app.Ceraceous
F
4

If you're looking for a UI to edit the settings from inside the app, check out InAppSettingsKit at http://www.inappsettingskit.com

Frunze answered 1/7, 2010 at 9:2 Comment(0)
H
0

Read the File and Networking Guide from the iPhone Developer Connection. It will explain how you get the path for the different predefined locations in the application sandbox. I'd recommend that you use a NSDictionary to store your preferences which can easily be saved to the file system and converted into a plist.

Hrvatska answered 9/2, 2010 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.