iOS: UserDefaults and Application Settings

NSUserDefaults are by default restricted to the application’s domain and are stored in the file $HOME/Library/Preferences/<ApplicationBundleIdentifer>.plist. You access the user defaults in code. To add user defaults to the in iOS, you must wrap the user defaults in a Settings Bundle.

1. NSUserDefaults stores the user settings in your app. Code or a custom UI without a Settings Bundle is used for frequently changing settings.
2. The Settings Bundle displays NSUserDefaults in the Settings app and is used for infrequently changing settings.

Add a Seetings Bundle by going to File > New > File and browse to the Resource > Settings file.


To create the following Settings


change the Settings Bundle you added with the following configuration


Read or create user defaults early in your application.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// to add a user default programmatically
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"test" forKey:@"environment"];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:appDefaults];

Then where you need your user default, read it as follows

NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
NSString *restapi = [[NSUserDefaults standardUserDefaults] stringForKey:@"restapi"];

Then in your code, you register default values using the registerDefaults: method. This method places your custom default values in the NSRegistrationDomain domain. Note that NSUserDefaults class caches values, which can be changed by other apps. Call the synchronize: method if you need to, or register for the notification NSUserDefaultsDidChangeNotification.


property list objects

Application Preservation and Restoration
Application Settings

Leave a Reply

Your email address will not be published. Required fields are marked *