The example in Listing 1-1 shows how you can use Preference Services to set an application's text color preference. This section builds upon that example to demonstrate how you would define text and window background color preferences that are shared among a suite of applications.
Saving a suite preference is quite similar to saving an application preference. The only difference is you use a suite ID in place of a domain qualifier constant. The code in Listing 1-5 demonstrates the process.
Listing 1-5 Saving a suite preference
CFStringRef suiteID = CFSTR("com.foosoft.powersuite"); CFStringRef defaultTextColorKey = CFSTR("defaultTextColor"); CFStringRef defaultWinBGColorKey = CFSTR("defaultWinBGColor"); CFStringRef colorBLUE = CFSTR("BLUE"); CFStringRef colorLBROWN = CFSTR("LIGHTBROWN"); // Set up the text color preference. CFPreferencesSetAppValue(defaultTextColorKey, colorBLUE, suiteID); // Set up the window background color preference. CFPreferencesSetAppValue(defaultWinBGColorKey, colorLBROWN, suiteID);
In order to add the suite preferences to your application's preference search chain, you simply call
CFPreferencesAddSuitePreferencesToApp
using the appropriate domain qualifier constant and suite ID as shown in Listing 1-6. This should be done only once early in the application's initialization phase.
Listing 1-6 Adding application suite preferences to the search chain
// Add the suite preferences to the current application's search chain. CFPreferencesAddSuitePreferencesToApp(kCFPreferencesCurrentApplication, CFSTR("com.foosoft.powersuite"));
You retrieve the stored preferences exactly as in the earlier examples. Because the code in Listing 1-6 adds the suite preferences into the search chain, no special work is required to locate the values stored in the suite preferences.
Listing 1-7 Retrieving application suite preferences
CFStringRef textColor; CFStringRef winBGColor; // Add the suite preferences to the current application's search chain. CFPreferencesAddSuitePreferencesToApp(kCFPreferencesCurrentApplication, CFSTR("com.foosoft.powersuite")); // Get the window background color preference. winBGColor = CFPreferencesCopyAppValue(defaultWinBGColorKey, kCFPreferencesCurrentApplication); // Get the text color preference. textColor = CFPreferencesCopyAppValue(defaultTextColorKey, kCFPreferencesCurrentApplication);