Sign in Join
Wiki copytemporary

Mac Preferences Read and Write: what changed

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

Title: Mac Preferences Read and Write

OldNewLine
1+*(macOS only)*
2+
3+## macOS File Storage Overview
4+
5+## CFPreferences
6+
7+For those who want to read and write preferences for a macOS application, this can be done using the following methods:
8+
9+| CODE FOR READING PREFERENCES |
10+|------------------------------|
11+| |
12+
13+``` pascal
14+ uses MacOSAll, CFPreferences;
15+
16+ var
17+ IsValid: Boolean; // On return indicates if key exists and has valid data
18+ Pref: Integer;
19+
20+ procedure TForm1.FormCreate(Sender: TObject);
21+ begin
22+ try
23+ Pref := CFPreferencesGetAppIntegerValue(CFStr('Check1'),kCFPreferencesCurrentApplication,IsValid);
24+ if (Pref = 1) then
25+ CheckBox1.Checked := true
26+ else
27+ CheckBox1.Checked := false;
28+
29+ Pref := CFPreferencesGetAppIntegerValue(CFStr('Check2'),kCFPreferencesCurrentApplication,IsValid);
30+ if (Pref = 1) then
31+ CheckBox2.Checked := true
32+ else
33+ CheckBox2.Checked := false;
34+
35+ Pref := CFPreferencesGetAppIntegerValue(CFStr('Check3'),kCFPreferencesCurrentApplication,IsValid);
36+ if (Pref = 1) then
37+ CheckBox3.Checked := true
38+ else
39+ CheckBox3.Checked := false;
40+
41+ Pref := CFPreferencesGetAppIntegerValue(CFStr('Check4'),kCFPreferencesCurrentApplication,IsValid);
42+ if (Pref = 1) then
43+ CheckBox4.Checked := true
44+ else
45+ CheckBox4.Checked := false;
46+
47+ except
48+ on E : Exception do
49+ ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
50+ end;
51+ end;
52+```
53+
54+| CODE FOR WRITING PREFERENCES |
55+|------------------------------|
56+| |
57+
58+``` pascal
59+ uses MacOSAll, CFPreferences;
60+
61+ var
62+ ItemName: CFStringRef;
63+ ItemVal: CFPropertyListRef;
64+
65+ procedure TForm1.FormClose(Sender: TObject);
66+ begin
67+ try
68+ if (CheckBox1.Checked) then
69+ begin
70+ ItemName := CFStr('Check1');
71+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
72+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
73+ end
74+ else
75+ begin
76+ ItemName := CFStr('Check1');
77+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
78+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
79+ end;
80+
81+ if (CheckBox2.Checked) then
82+ begin
83+ ItemName := CFStr('Check2');
84+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
85+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
86+ end
87+ else
88+ begin
89+ ItemName := CFStr('Check2');
90+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
91+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
92+ end;
93+
94+ if (CheckBox3.Checked) then
95+ begin
96+ ItemName := CFStr('Check3');
97+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
98+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
99+ end
100+ else
101+ begin
102+ ItemName := CFStr('Check3');
103+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
104+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
105+ end;
106+
107+ if (CheckBox4.Checked) then
108+ begin
109+ ItemName := CFStr('Check4');
110+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'1',kCFStringEncodingUTF8);
111+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
112+ end
113+ else
114+ begin
115+ ItemName := CFStr('Check4');
116+ ItemVal := CFStringCreateWithPascalString(kCFAllocatorDefault,'0',kCFStringEncodingUTF8);
117+ CFPreferencesSetAppValue(ItemName,ItemVal,kCFPreferencesCurrentApplication);
118+ end;
119+
120+ // write out the preference data
121+ CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
122+
123+ except
124+ on E : Exception do
125+ ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
126+ end;
127+```
128+
129+<figure>
130+<img src="Prefs1.png" title="Prefs1.png" />
131+<figcaption>Prefs1.png</figcaption>
132+</figure>
133+
134+## NSUserDefaults
135+
136+The 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.
137+
138+Unfortunately 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).
139+
140+## See also
141+
142+- [[macOS Form Position Save and Restore]] - Automatic save and restore using just one line of code!
143+- [[Introduction to platform-sensitive development]]
144+- [[Multiplatform Programming Guide|Proper macOS file locations]]
145+- [[macOS Programming Tips]]
146+
147+## External links
148+
149+- [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)
150+- [Apple: NSUserDefaults](https://developer.apple.com/documentation/foundation/nsuserdefaults)
151+- [Apple: Preferences](https://developer.apple.com/documentation/foundation/preferences)
152+- [Apple: Preference Utilities](https://developer.apple.com/documentation/corefoundation/preferences_utilities)
153+- [Apple: Preferences and Settings Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/UserDefaults/Introduction/Introduction.html)
154+- [Apple: Human Interface Guidelines - Preferences](https://developer.apple.com/design/human-interface-guidelines/macos/app-architecture/preferences/)
155+
156+---
157+*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