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