| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in iPhone OS 2.0 and later. |
| Companion guide | |
| Declared in | NSUserDefaults.h |
| Related sample code |
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.
At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. 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. For more details, see User Defaults Programming Topics for Cocoa.
Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for "MyStringDefault", the string you later retrieve using stringForKey: will be immutable.
A defaults database is created automatically for each user. The NSUserDefaults class does not currently support per-host preferences. To do this, you must use the CFPreferences API (see Preferences Utilities Reference). However, NSUserDefaults correctly reads per-host preferences, so you can safely mix CFPreferences code with NSUserDefaults code.
If your application supports managed environments, you can use an NSUserDefaults object to determine which preferences are managed by an administrator for the benefit of the user. Managed environments correspond to computer labs or classrooms where an administrator or teacher may want to configure the systems in a particular way. In these situations, the teacher can establish a set of default preferences and force those preferences on users. If a preference is managed in this manner, applications should prevent users from editing that preference by disabling any appropriate controls.
The NSUserDefaults class is thread-safe.
When using NSURL instances to refer to files within a process, it's important to make the distinction between location-based tracking (file: scheme URLs that are basically paths) versus filesystem identity tracking (file: scheme URLs that are file reference URLs). When persisting an NSURL, you should take that behavior into consideration. If your application tracks the resource being located by its identity so that it can be found if the user moves the file, then you should explicitly write the NSURL's bookmark data or encode a file reference URL.
If you want to track a file by reference but you require explicit control over when resolution occurs, you should take care to write out bookmark data to NSUserDefaults rather than rely on -[NSUserDefaults setURL:forKey:]. This allows you to call +[NSURL URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:] at a time when you know your application will be able to handle the potential I/O or required user interface interactions.
– arrayForKey:
– boolForKey:
– dataForKey:
– dictionaryForKey:
– floatForKey:
– integerForKey:
– objectForKey:
– stringArrayForKey:
– stringForKey:
– doubleForKey:
– synchronize
– persistentDomainForName:
– persistentDomainNames
– removePersistentDomainForName:
– setPersistentDomain:forName:
– removeVolatileDomainForName:
– setVolatileDomain:forName:
– volatileDomainForName:
– volatileDomainNames
Synchronizes any changes made to the shared user defaults object and releases it from memory.
+ (void)resetStandardUserDefaults
A subsequent invocation of standardUserDefaults creates a new shared user defaults object with the standard search list.
NSUserDefaults.hReturns the shared defaults object.
+ (NSUserDefaults *)standardUserDefaults
The shared defaults object.
If the shared defaults object does not exist yet, it is created with a search list containing the names of the following domains, in this order:
NSArgumentDomain, consisting of defaults parsed from the application’s arguments
A domain identified by the application’s bundle identifier
NSGlobalDomain, consisting of defaults meant to be seen by all applications
Separate domains for each of the user’s preferred languages
NSRegistrationDomain, a set of temporary defaults whose values can be set by the application to ensure that searches will always be successful
The defaults are initialized for the current user. Subsequent modifications to the standard search list remain in effect even when this method is invoked again—the search list is guaranteed to be standard only the first time this method is invoked. The shared instance is provided as a convenience—you can create custom instances using alloc along with initWithUser: or init.
NSUserDefaults.hInserts the specified domain name into the receiver’s search list.
- (void)addSuiteNamed:(NSString *)suiteName
The domain name to insert. This domain is inserted after the application domain.
The suiteName domain is similar to a bundle identifier string, but is not tied to a particular application or bundle. A suite can be used to hold preferences that are shared between multiple applications.
NSUserDefaults.hReturns the array associated with the specified key.
- (NSArray *)arrayForKey:(NSString *)defaultName
A key in the current user's defaults database.
The array associated with the specified key, or nil if the key does not exist or its value is not an NSArray object.
The returned array and its contents are immutable, even if the values you originally set were mutable.
NSUserDefaults.hReturns the Boolean value associated with the specified key.
- (BOOL)boolForKey:(NSString *)defaultName
A key in the current user's defaults database.
If a boolean value is associated with defaultName in the user defaults, that value is returned. Otherwise, NO is returned.
NSUserDefaults.hReturns the data object associated with the specified key.
- (NSData *)dataForKey:(NSString *)defaultName
A key in the current user's defaults database.
The data object associated with the specified key, or nil if the key does not exist or its value is not an NSData object.
The returned data object is immutable, even if the value you originally set was a mutable data object.
NSUserDefaults.hReturns the dictionary object associated with the specified key.
- (NSDictionary *)dictionaryForKey:(NSString *)defaultName
A key in the current user's defaults database.
The dictionary object associated with the specified key, or nil if the key does not exist or its value is not an NSDictionary object.
The returned dictionary and its contents are immutable, even if the values you originally set were mutable.
NSUserDefaults.hReturns a dictionary that contains a union of all key-value pairs in the domains in the search list.
- (NSDictionary *)dictionaryRepresentation
A dictionary containing the keys. The keys are names of defaults and the value corresponding to each key is a property list object (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).
As with objectForKey:, key-value pairs in domains that are earlier in the search list take precedence. The combined result does not preserve information about which domain each entry came from.
NSUserDefaults.hReturns the double value associated with the specified key.
- (double)doubleForKey:(NSString *)defaultName
A key in the current user's defaults database.
The double value associated with the specified key. If the key does not exist, this method returns 0.
NSUserDefaults.hReturns the floating-point value associated with the specified key.
- (float)floatForKey:(NSString *)defaultName
A key in the current user's defaults database.
The floating-point value associated with the specified key. If the key does not exist, this method returns 0.
NSUserDefaults.hReturns an NSUserDefaults object initialized with the defaults for the current user account.
- (id)init
An initialized NSUserDefaults object whose argument and registration domains are already set up.
This method does not put anything in the search list. Invoke it only if you’ve allocated your own NSUserDefaults instance instead of using the shared one.
NSUserDefaults.hReturns an NSUserDefaults object initialized with the defaults for the specified user account.
- (id)initWithUser:(NSString *)username
The name of the user account.
An initialized NSUserDefaults object whose argument and registration domains are already set up. If the current user does not have access to the specified user account, this method returns nil.
This method does not put anything in the search list. Invoke it only if you’ve allocated your own NSUserDefaults instance instead of using the shared one.
You do not normally use this method to initialize an instance of NSUserDefaults. Applications used by a superuser might use this method to update the defaults databases for a number of users. The user who started the application must have appropriate access (read, write, or both) to the defaults database of the new user, or this method returns nil.
NSUserDefaults.hReturns the integer value associated with the specified key..
- (NSInteger)integerForKey:(NSString *)defaultName
A key in the current user's defaults database.
The integer value associated with the specified key. If the specified key does not exist, this method returns 0.
NSUserDefaults.hReturns the object associated with the first occurrence of the specified default.
- (id)objectForKey:(NSString *)defaultName
A key in the current user's defaults database.
The object associated with the specified key, or nil if the key was not found.
This method searches the domains included in the search list in the order they are listed.
The returned object is immutable, even if the value you originally set was mutable.
NSUserDefaults.hReturns a Boolean value indicating whether the specified key is managed by an administrator.
- (BOOL)objectIsForcedForKey:(NSString *)key
The key whose status you want to check.
YES if the value of the specified key is managed by an administrator, otherwise NO.
This method assumes that the key is a preference associated with the current user and application. For managed keys, the application should disable any user interface that allows the user to modify the value of key.
NSUserDefaults.hReturns a Boolean value indicating whether the key in the specified domain is managed by an administrator.
- (BOOL)objectIsForcedForKey:(NSString *)key inDomain:(NSString *)domain
The key whose status you want to check.
The domain of the key.
YES if the key is managed by an administrator in the specified domain, otherwise NO.
This method assumes that the key is a preference associated with the current user. For managed keys, the application should disable any user interface that allows the user to modify the value of key.
NSUserDefaults.hReturns a dictionary containing the keys and values in the specified persistent domain.
- (NSDictionary *)persistentDomainForName:(NSString *)domainName
The domain whose keys and values you want. This value should be equal to your application's bundle identifier.
A dictionary containing the keys. The keys are names of defaults and the value corresponding to each key is a property list object (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).
NSUserDefaults.hReturns an array of the current persistent domain names.
- (NSArray *)persistentDomainNames
An array of NSString objects containing the domain names.
You can get the keys and values for each domain by passing the returned domain names to the persistentDomainForName: method.
NSUserDefaults.hAdds the contents the specified dictionary to the registration domain.
- (void)registerDefaults:(NSDictionary *)dictionary
The dictionary of keys and values you want to register.
If there is no registration domain, one is created using the specified dictionary, and NSRegistrationDomain is added to the end of the search list.
The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.
NSUserDefaults.hRemoves the value of the specified default key in the standard application domain.
- (void)removeObjectForKey:(NSString *)defaultName
The key whose value you want to remove.
Removing a default has no effect on the value returned by the objectForKey: method if the same key exists in a domain that precedes the standard application domain in the search list.
NSUserDefaults.hRemoves the contents of the specified persistent domain from the user’s defaults.
- (void)removePersistentDomainForName:(NSString *)domainName
The domain whose keys and values you want. This value should be equal to your application's bundle identifier.
When a persistent domain is changed, an NSUserDefaultsDidChangeNotification is posted.
NSUserDefaults.hRemoves the specified domain name from the receiver’s search list.
- (void)removeSuiteNamed:(NSString *)suiteName
The domain name to remove.
NSUserDefaults.hRemoves the specified volatile domain from the user’s defaults.
- (void)removeVolatileDomainForName:(NSString *)domainName
The volatile domain you want to remove.
NSUserDefaults.hSets the value of the specified default key to the specified Boolean value.
- (void)setBool:(BOOL)value forKey:(NSString *)defaultName
The Boolean value to store in the defaults database.
The key with which to associate with the value.
Invokes setObject:forKey: as part of its implementation.
NSUserDefaults.hSets the value of the specified default key to the double value.
- (void)setDouble:(double)value forKey:(NSString *)defaultName
The double value.
The key with which to associate with the value.
NSUserDefaults.hSets the value of the specified default key to the specified floating-point value.
- (void)setFloat:(float)value forKey:(NSString *)defaultName
The floating-point value to store in the defaults database.
The key with which to associate with the value.
Invokes setObject:forKey: as part of its implementation.
NSUserDefaults.hSets the value of the specified default key to the specified integer value.
- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName
The integer value to store in the defaults database.
The key with which to associate with the value.
Invokes setObject:forKey: as part of its implementation.
NSUserDefaults.hSets the value of the specified default key in the standard application domain.
- (void)setObject:(id)value forKey:(NSString *)defaultName
The object to store in the defaults database. A default’s value can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.
The key with which to associate with the value.
Setting a default has no effect on the value returned by the objectForKey: method if the same key exists in a domain that precedes the application domain in the search list.
NSUserDefaults.hSets the dictionary for the specified persistent domain.
- (void)setPersistentDomain:(NSDictionary *)domain forName:(NSString *)domainName
The dictionary of keys and values you want to assign to the domain.
The domain whose keys and values you want to set. This value should be equal to your application's bundle identifier.
When a persistent domain is changed, an NSUserDefaultsDidChangeNotification is posted.
NSUserDefaults.hSets the dictionary for the specified volatile domain.
- (void)setVolatileDomain:(NSDictionary *)domain forName:(NSString *)domainName
The dictionary of keys and values you want to assign to the domain.
The domain whose keys and values you want to set.
This method raises an NSInvalidArgumentException if a volatile domain with the specified name already exists.
NSUserDefaults.hReturns the array of strings associated with the specified key.
- (NSArray *)stringArrayForKey:(NSString *)defaultName
A key in the current user's defaults database.
The array of NSString objects, or nil if the specified default does not exist, the default does not contain an array, or the array does not contain NSString objects.
The returned array and its contents are immutable, even if the values you originally set were mutable.
NSUserDefaults.hReturns the string associated with the specified key.
- (NSString *)stringForKey:(NSString *)defaultName
A key in the current user's defaults database.
The string associated with the specified key, or nil if the default does not exist or does not contain a string.
The returned string is immutable, even if the value you originally set was a mutable string.
NSUserDefaults.hWrites any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.
- (BOOL)synchronize
YES if the data was saved successfully to disk, otherwise NO.
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.
– persistentDomainForName:– persistentDomainNames– removePersistentDomainForName:– setPersistentDomain:forName:NSUserDefaults.hReturns the dictionary for the specified volatile domain.
- (NSDictionary *)volatileDomainForName:(NSString *)domainName
The domain whose keys and values you want.
The dictionary of keys and values belonging to the domain. The keys in the dictionary are names of defaults, and the value corresponding to each key is a property list object (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).
NSUserDefaults.hReturns an array of the current volatile domain names.
- (NSArray *)volatileDomainNames
An array of NSString objects with the volatile domain names.
You can get the contents of each domain by passing the returned domain names to the volatileDomainForName: method.
NSUserDefaults.hThese constants specify various user defaults domains.
extern NSString *NSGlobalDomain; extern NSString *NSArgumentDomain; extern NSString *NSRegistrationDomain;
NSGlobalDomainThe domain consisting of defaults meant to be seen by all applications.
Available in iPhone OS 2.0 and later.
Declared in NSUserDefaults.h.
NSArgumentDomainThe domain consisting of defaults parsed from the application’s arguments. These are one or more pairs of the form -default value included in the command-line invocation of the application.
Available in iPhone OS 2.0 and later.
Declared in NSUserDefaults.h.
NSRegistrationDomainThe domain consisting of a set of temporary defaults whose values can be set by the application to ensure that searches will always be successful.
Available in iPhone OS 2.0 and later.
Declared in NSUserDefaults.h.
NSUserDefaults.hThis notification is posted when a change is made to defaults in a persistent domain.
The notification object is the NSUserDefaults object. This notification does not contain a userInfo dictionary.
NSUserDefaults.hLast updated: 2009-08-28