Preferences Utilities Reference
Core Foundation provides a simple, standard way to manage user (and application) preferences. Core Foundation stores preferences as key-value pairs that are assigned a scope using a combination of user name, application ID, and host (computer) names. This makes it possible to save and retrieve preferences that apply to different classes of users. Core Foundation preferences is useful to all applications that support user preferences. Note that modification of some preferences domains (those not belonging to the “Current User”) requires root privileges (or Admin privileges prior to OS X v10.6)—see Authorization Services Programming Guide for information on how to gain suitable privileges.
Unlike some other Core Foundation types, CFPreferences is not toll-free bridged to its corresponding Cocoa Foundation framework class (NSUserDefaults). CFPreferences is thread-safe.
-
Obtains a preference value for the specified key and application.
Declaration
Swift
func CFPreferencesCopyAppValue(_key: CFString!, _applicationID: CFString!) -> CFPropertyList!Objective-C
CFPropertyListRef CFPreferencesCopyAppValue ( CFStringRef key, CFStringRef applicationID );Parameters
keyThe preference key whose value to obtain.
applicationIDThe identifier of the application whose preferences to search, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name,com.foosoft.Return Value
The preference data for the specified key and application. If no value was located, returns
NULL. Ownership follows the Create Rule.Discussion
Note that values returned from this function are immutable, even if you have recently set the value using a mutable object.
Availability
Available in iOS 2.0 and later.
-
Constructs and returns the list of all keys set in the specified domain.
Declaration
Swift
func CFPreferencesCopyKeyList(_applicationID: CFString!, _userName: CFString!, _hostName: CFString!) -> CFArray!Objective-C
CFArrayRef CFPreferencesCopyKeyList ( CFStringRef applicationID, CFStringRef userName, CFStringRef hostName );Parameters
applicationIDThe ID of the application whose preferences to search. Takes the form of a Java package name,
com.foosoft.userNamekCFPreferencesCurrentUserto search the current-user domain, otherwisekCFPreferencesAnyUserto search the any-user domain.hostNamekCFPreferencesCurrentHostto search the current-host domain, otherwisekCFPreferencesAnyHostto search the any-host domain.Return Value
The list of keys. Ownership follows the Create Rule.
Availability
Available in iOS 2.0 and later.
-
Returns a dictionary containing preference values for multiple keys.
Declaration
Swift
func CFPreferencesCopyMultiple(_keysToFetch: CFArray!, _applicationID: CFString!, _userName: CFString!, _hostName: CFString!) -> CFDictionary!Objective-C
CFDictionaryRef CFPreferencesCopyMultiple ( CFArrayRef keysToFetch, CFStringRef applicationID, CFStringRef userName, CFStringRef hostName );Parameters
keysToFetchAn array of preference keys the values of which to obtain.
applicationIDThe ID of the application whose preferences are searched. Takes the form of a Java package name, such as
com.foosoft.userNamekCFPreferencesCurrentUserto search the current-user domain, otherwisekCFPreferencesAnyUserto search the any-user domain.hostNamekCFPreferencesCurrentHostto search the current-host domain, otherwisekCFPreferencesAnyHostto search the any-host domain.Return Value
A dictionary containing the preference values for the keys specified by
keysToFetchfor the specified domain. If no values were located, returns an empty dictionary. Ownership follows the Create Rule.Discussion
Note that values returned from this function are immutable, even if you have recently set the value using a mutable object.
Availability
Available in iOS 2.0 and later.
-
Returns a preference value for a given domain.
Declaration
Swift
func CFPreferencesCopyValue(_key: CFString!, _applicationID: CFString!, _userName: CFString!, _hostName: CFString!) -> CFPropertyList!Objective-C
CFPropertyListRef CFPreferencesCopyValue ( CFStringRef key, CFStringRef applicationID, CFStringRef userName, CFStringRef hostName );Parameters
keyPreferences key for the value to obtain.
applicationIDThe ID of the application whose preferences are searched. Takes the form of a Java package name, such as
com.foosoft.userNamekCFPreferencesCurrentUserif to search the current-user domain, otherwisekCFPreferencesAnyUserto search the any-user domain.hostNamekCFPreferencesCurrentHostif to search the current-host domain, otherwisekCFPreferencesAnyHostto search the any-host domain.Return Value
The preference data for the specified domain. If the no value was located, returns
NULL. Ownership follows the Create Rule.Discussion
This function is the primitive get mechanism for the higher level preference function
CFPreferencesCopyAppValueUnlike the high-level function,CFPreferencesCopyValuesearches only the exact domain specified. Do not use this function directly unless you have a need. All arguments must be non-NULL. Do not use arbitrary user and host names, instead pass the pre-defined domain qualifier constants.Note that values returned from this function are immutable, even if you have recently set the value using a mutable object.
Availability
Available in iOS 2.0 and later.
-
Convenience function that directly obtains a boolean preference value for the specified key.
Declaration
Swift
func CFPreferencesGetAppBooleanValue(_key: CFString!, _applicationID: CFString!, _keyExistsAndHasValidFormat: UnsafeMutablePointer<DarwinBoolean>) -> BoolObjective-C
Boolean CFPreferencesGetAppBooleanValue ( CFStringRef key, CFStringRef applicationID, Boolean *keyExistsAndHasValidFormat );Parameters
keyThe preference key whose value to obtain. The key must specify a preference whose value is of type
Boolean.applicationIDThe identifier of the application whose preferences are searched, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name, such ascom.foosoft.keyExistsAndHasValidFormatOn return,
trueif the preference value for the specified key was located and found to be of typeBoolean, otherwisefalse.Return Value
The preference data for the specified key and application, or if no value was located,
false.Availability
Available in iOS 2.0 and later.
-
Convenience function that directly obtains an integer preference value for the specified key.
Declaration
Swift
func CFPreferencesGetAppIntegerValue(_key: CFString!, _applicationID: CFString!, _keyExistsAndHasValidFormat: UnsafeMutablePointer<DarwinBoolean>) -> CFIndexObjective-C
CFIndex CFPreferencesGetAppIntegerValue ( CFStringRef key, CFStringRef applicationID, Boolean *keyExistsAndHasValidFormat );Parameters
keyThe preference key whose value you wish to obtain. The key must specify a preference whose value is of type
int.applicationIDThe identifier of the application whose preferences you wish to search, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name,com.foosoft.keyExistsAndHasValidFormatOn return, indicates whether the preference value for the specified key was located and found to be of type
int.Return Value
The preference data for the specified key and application. If no value was located,
0is returned.Availability
Available in iOS 2.0 and later.
-
Adds, modifies, or removes a preference.
Declaration
Swift
func CFPreferencesSetAppValue(_key: CFString!, _value: CFPropertyList!, _applicationID: CFString!)Objective-C
void CFPreferencesSetAppValue ( CFStringRef key, CFPropertyListRef value, CFStringRef applicationID );Parameters
keyThe preference key whose value you wish to set.
valueThe value to set for the specified
keyand application. PassNULLto remove the specified key from the application’s preferences.applicationIDThe ID of the application whose preferences you wish to create or modify, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name,com.foosoft.Discussion
New preference values are stored in the standard application preference location,
~/Library/Preferences/. When called withkCFPreferencesCurrentApplication, modifications are performed in the preference domain “Current User, Current Application, Any Host.” If you need to create preferences in some other domain, use the low-level functionCFPreferencesSetValue.You must call the
CFPreferencesAppSynchronizefunction in order for your changes to be saved to permanent storage.Availability
Available in iOS 2.0 and later.
-
Convenience function that allows you to set and remove multiple preference values.
Declaration
Swift
func CFPreferencesSetMultiple(_keysToSet: CFDictionary!, _keysToRemove: CFArray!, _applicationID: CFString!, _userName: CFString!, _hostName: CFString!)Objective-C
void CFPreferencesSetMultiple ( CFDictionaryRef keysToSet, CFArrayRef keysToRemove, CFStringRef applicationID, CFStringRef userName, CFStringRef hostName );Parameters
keysToSetA dictionary containing the key/value pairs for the preferences to set.
keysToRemoveAn array containing a list of keys to remove.
applicationIDThe ID of the application whose preferences you wish to modify. Takes the form of a Java package name,
com.foosoft.userNamekCFPreferencesCurrentUserto modify the current user’s preferences, otherwisekCFPreferencesAnyUserto modify the preferences of all users.hostNamekCFPreferencesCurrentHostto modify the preferences of the current host, otherwisekCFPreferencesAnyHostto modify the preferences of all hosts.Discussion
Behavior is undefined if a key is in both
keysToSetandkeysToRemoveAvailability
Available in iOS 2.0 and later.
-
Adds, modifies, or removes a preference value for the specified domain.
Declaration
Swift
func CFPreferencesSetValue(_key: CFString!, _value: CFPropertyList!, _applicationID: CFString!, _userName: CFString!, _hostName: CFString!)Objective-C
void CFPreferencesSetValue ( CFStringRef key, CFPropertyListRef value, CFStringRef applicationID, CFStringRef userName, CFStringRef hostName );Parameters
keyPreferences key for the value you wish to set.
valueThe value to set for
keyand application. PassNULLto removekeyfrom the domain.applicationIDThe ID of the application whose preferences you wish to modify. Takes the form of a Java package name,
com.foosoft.userNamekCFPreferencesCurrentUserto modify the current user’s preferences, otherwisekCFPreferencesAnyUserto modify the preferences of all users.hostNamekCFPreferencesCurrentHostto modify the preferences of the current host, otherwisekCFPreferencesAnyHostto modify the preferences of all hosts.Discussion
This function is the primitive set mechanism for the higher level preference function
CFPreferencesSetAppValue. Only the exact domain specified is modified. Do not use this function directly unless you have a specific need. All arguments exceptvaluemust be non-NULL. Do not use arbitrary user and host names, instead pass the pre-defined constants.You must call the
CFPreferencesSynchronizefunction in order for your changes to be saved to permanent storage. Note that you can only save preferences for “Any User” if you have root privileges (or Admin privileges prior to OS X v10.6).Availability
Available in iOS 2.0 and later.
-
Writes to permanent storage all pending changes to the preference data for the application, and reads the latest preference data from permanent storage.
Declaration
Swift
func CFPreferencesAppSynchronize(_applicationID: CFString!) -> BoolObjective-C
Boolean CFPreferencesAppSynchronize ( CFStringRef applicationID );Parameters
applicationIDThe ID of the application whose preferences to write to storage, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name,com.foosoft.Return Value
trueif synchronization was successful, otherwisefalse.Discussion
Calling the function
CFPreferencesSetAppValueis not in itself sufficient for storing preferences. TheCFPreferencesAppSynchronizefunction writes to permanent storage all pending preference changes for the application. Typically you would call this function after multiple calls toCFPreferencesSetAppValue. Conversely, preference data is cached after it is first read. Changes made externally are not automatically incorporated. TheCFPreferencesAppSynchronizefunction reads the latest preferences from permanent storage.Availability
Available in iOS 2.0 and later.
-
For the specified domain, writes all pending changes to preference data to permanent storage, and reads latest preference data from permanent storage.
Declaration
Swift
func CFPreferencesSynchronize(_applicationID: CFString!, _userName: CFString!, _hostName: CFString!) -> BoolObjective-C
Boolean CFPreferencesSynchronize ( CFStringRef applicationID, CFStringRef userName, CFStringRef hostName );Parameters
applicationIDThe ID of the application whose preferences you wish to modify. Takes the form of a Java package name,
com.foosoft.userNamekCFPreferencesCurrentUserto modify the current user’s preferences, otherwisekCFPreferencesAnyUserto modify the preferences of all users.hostNamekCFPreferencesCurrentHostto search the current-host domain, otherwisekCFPreferencesAnyHostto search the any-host domain.Return Value
trueif synchronization was successful,falseif an error occurred.Discussion
This function is the primitive synchronize mechanism for the higher level preference function
CFPreferencesAppSynchronize; it writes updated preferences to permanent storage, and reads the latest preferences from permanent storage. Only the exact domain specified is modified. Note that to modify “Any User” preferences requires root privileges (or Admin privileges prior to OS X v10.6)—see Authorization Services Programming Guide.Do not use this function directly unless you have a specific need. All arguments must be non-
NULL. Do not use arbitrary user and host names, instead pass the pre-defined constants.Availability
Available in iOS 2.0 and later.
-
Adds suite preferences to an application’s preference search chain.
Declaration
Swift
func CFPreferencesAddSuitePreferencesToApp(_applicationID: CFString!, _suiteID: CFString!)Objective-C
void CFPreferencesAddSuitePreferencesToApp ( CFStringRef applicationID, CFStringRef suiteID );Parameters
applicationIDThe ID of the application to which to add suite preferences, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name,com.foosoft.suiteIDThe ID of the application suite preferences to add. Takes the form of a Java package name,
com.foosoft.Discussion
Suite preferences allow you to maintain a set of preferences that are common to all applications in the suite. When a suite is added to an application’s search chain, all of the domains pertaining to that suite are inserted into the chain. Suite preferences are added between the “Current Application” domains and the “Any Application” domains. If you add multiple suite preferences to one application, the order of the suites in the search chain is non-deterministic. You can override a suite preference for a given application by defining the same preference key in the application specific preferences.
Availability
Available in iOS 2.0 and later.
-
Removes suite preferences from an application’s search chain.
Declaration
Swift
func CFPreferencesRemoveSuitePreferencesFromApp(_applicationID: CFString!, _suiteID: CFString!)Objective-C
void CFPreferencesRemoveSuitePreferencesFromApp ( CFStringRef applicationID, CFStringRef suiteID );Parameters
applicationIDThe ID of the application from which to remove suite preferences, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name,com.foosoft.suiteIDThe ID of the application suite preferences to remove. Takes the form of a Java package name,
com.foosoft.Availability
Available in iOS 2.0 and later.
-
Determines whether or not a given key has been imposed on the user.
Declaration
Swift
func CFPreferencesAppValueIsForced(_key: CFString!, _applicationID: CFString!) -> BoolObjective-C
Boolean CFPreferencesAppValueIsForced ( CFStringRef key, CFStringRef applicationID );Parameters
keyThe key you are querying.
applicationIDThe application’s ID, typically
kCFPreferencesCurrentApplication. Do not passNULLorkCFPreferencesAnyApplication. Takes the form of a Java package name,com.foosoft.Return Value
trueif value of the key cannot be changed by the user, otherwisefalse.Discussion
In cases where machines and/or users are under some kind of management, you should use this function to determine whether or not to disable UI elements corresponding to those preference keys.
Availability
Available in iOS 2.0 and later.
-
CFPreferencesCopyApplicationList(iOS 7.0)Constructs and returns the list of all applications that have preferences in the scope of the specified user and host.
Declaration
Objective-C
CFArrayRef CFPreferencesCopyApplicationList ( CFStringRef userName, CFStringRef hostName );Parameters
userNamekCFPreferencesCurrentUserto search the current-user domain, otherwisekCFPreferencesAnyUserto search the any-user domain.hostNamekCFPreferencesCurrentHostto search the current-host domain, otherwisekCFPreferencesAnyHostto search the any-host domain.Return Value
The list of application IDs. Ownership follows the Create Rule.
Availability
Available in iOS 2.0 and later.
Deprecated in iOS 7.0.
-
Keys used to specify the common preference domains.
Declaration
Swift
let kCFPreferencesAnyApplication: CFString! let kCFPreferencesAnyHost: CFString! let kCFPreferencesAnyUser: CFString! let kCFPreferencesCurrentApplication: CFString! let kCFPreferencesCurrentHost: CFString! let kCFPreferencesCurrentUser: CFString!Objective-C
const CFStringRef kCFPreferencesAnyApplication; const CFStringRef kCFPreferencesAnyHost; const CFStringRef kCFPreferencesAnyUser; const CFStringRef kCFPreferencesCurrentApplication; const CFStringRef kCFPreferencesCurrentHost; const CFStringRef kCFPreferencesCurrentUser;Constants
-
kCFPreferencesAnyApplicationkCFPreferencesAnyApplicationIndicates a preference that applies to any application.
Available in iOS 2.0 and later.
-
kCFPreferencesAnyHostkCFPreferencesAnyHostIndicates a preference that applies to any host.
This option is not supported.
Available in iOS 2.0 and later.
-
kCFPreferencesAnyUserkCFPreferencesAnyUserIndicates a preference that applies to any user.
Available in iOS 2.0 and later.
-
kCFPreferencesCurrentApplicationkCFPreferencesCurrentApplicationIndicates a preference that applies only to the current application.
Available in iOS 2.0 and later.
-
kCFPreferencesCurrentHostkCFPreferencesCurrentHostIndicates a preference that applies only to the current host.
Available in iOS 2.0 and later.
-
kCFPreferencesCurrentUserkCFPreferencesCurrentUserIndicates a preference that applies only to the current user.
Available in iOS 2.0 and later.
Discussion
Not all combinations of application, host, and user are supported preference domains. Specifically,
kCFPreferencesAnyHostis not supported in any combination with other options. -
Copyright © 2015 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2013-08-08
