Sign in Join
Wiki copytemporary

Mac Preferences Read and Write: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11*(macOS only)*
22
33## macOS File Storage Overview
44
55## CFPreferences
66
77For those who want to read and write preferences for a macOS application, this can be done using the following methods:
88
99| CODE FOR READING PREFERENCES |
1010|------------------------------|
1111| |
1212
1313``` pascal
1414 uses MacOSAll, CFPreferences;
1515
1616 var
1717 IsValid: Boolean; // On return indicates if key exists and has valid data
1818 Pref: Integer;
1919
2020 procedure TForm1.FormCreate(Sender: TObject);
2121 begin
2222 try
2323 Pref := CFPreferencesGetAppIntegerValue(CFStr('Check1'),kCFPreferencesCurrentApplication,IsValid);
2424 if (Pref = 1) then
2525 CheckBox1.Checked := true
2626 else
2727 CheckBox1.Checked := false;
2828
2929 Pref := CFPreferencesGetAppIntegerValue(CFStr('Check2'),kCFPreferencesCurrentApplication,IsValid);
3030 if (Pref = 1) then
3131 CheckBox2.Checked := true
3232 else
3333 CheckBox2.Checked := false;
3434
3535 Pref := CFPreferencesGetAppIntegerValue(CFStr('Check3'),kCFPreferencesCurrentApplication,IsValid);
3636 if (Pref = 1) then
3737 CheckBox3.Checked := true
3838 else
3939 CheckBox3.Checked := false;
4040
4141 Pref := CFPreferencesGetAppIntegerValue(CFStr('Check4'),kCFPreferencesCurrentApplication,IsValid);
4242 if (Pref = 1) then
4343 CheckBox4.Checked := true
4444 else
4545 CheckBox4.Checked := false;
4646
4747 except
4848 on E : Exception do
4949 ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
5050 end;
5151 end;
5252```
5353
5454| CODE FOR WRITING PREFERENCES |
5555|------------------------------|
5656| |
5757
5858``` pascal
5959 uses MacOSAll, CFPreferences;
6060
6161 var
6262 ItemName: CFStringRef;
6363 ItemVal: CFPropertyListRef;
6464
6565 procedure TForm1.FormClose(Sender: TObject);
6666 begin
6767 try
6868 if (CheckBox1.Checked) then
6969 begin
7070 ItemName := CFStr('Check1');
7171 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
7272 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
7373 end
7474 else
7575 begin
7676 ItemName := CFStr('Check1');
7777 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
7878 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
7979 end;
8080
8181 if (CheckBox2.Checked) then
8282 begin
8383 ItemName := CFStr('Check2');
8484 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
8585 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
8686 end
8787 else
8888 begin
8989 ItemName := CFStr('Check2');
9090 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
9191 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
9292 end;
9393
9494 if (CheckBox3.Checked) then
9595 begin
9696 ItemName := CFStr('Check3');
9797 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
9898 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
9999 end
100100 else
101101 begin
102102 ItemName := CFStr('Check3');
103103 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
104104 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
105105 end;
106106
107107 if (CheckBox4.Checked) then
108108 begin
109109 ItemName := CFStr('Check4');
110110 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
111111 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
112112 end
113113 else
114114 begin
115115 ItemName := CFStr('Check4');
116116 ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
117117 CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
118118 end;
119119
120120 // write out the preference data
121121 CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
122122
123123 except
124124 on E : Exception do
125125 ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
126126 end;
127127```
128128
129-<figure>
130-<img src="Prefs1.png" title="Prefs1.png" />
131-<figcaption>Prefs1.png</figcaption>
132-</figure>
129+![](/wiki-images/6/61/Prefs1.png)
133130
134131## NSUserDefaults
135132
136133The NSUserDefaults class, available since Mac OS X 10.0 (Cheetah), provides a programmatic interface for interacting with the defaults system which allows an application to customise its behaviour to match a user’s preferences.
137134
138135Unfortunately there were serious problems with NSUserdefaults in iOS 9 and macOS 10.12 (Sierra) and 10.13 (High Sierra) which mitigate against its use. Some of the issues are mentioned in this [Apple Developer Forum thread](https://developer.apple.com/forums/thread/88811).
139136
140137## See also
141138
142139- [[macOS Form Position Save and Restore]] - Automatic save and restore using just one line of code!
143140- [[Introduction to platform-sensitive development]]
144141- [[Multiplatform Programming Guide|Proper macOS file locations]]
145142- [[macOS Programming Tips]]
146143
147144## External links
148145
149146- [Apple: Managing Preferences Using Core Foundation](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/AccessingPreferenceValues/AccessingPreferenceValues.html#//apple_ref/doc/uid/10000059i-CH3-108766)
150147- [Apple: NSUserDefaults](https://developer.apple.com/documentation/foundation/nsuserdefaults)
151148- [Apple: Preferences](https://developer.apple.com/documentation/foundation/preferences)
152149- [Apple: Preference Utilities](https://developer.apple.com/documentation/corefoundation/preferences_utilities)
153150- [Apple: Preferences and Settings Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/Introduction/Introduction.html)
154151- [Apple: Human Interface Guidelines - Preferences](https://developer.apple.com/design/human-interface-guidelines/macos/app-architecture/preferences/)
155152
156153---
157154*From the Free Pascal wiki, page "Mac Preferences Read and Write", as it stood on 2021-12-28 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history