I have a SwiftUI sandbox app that uses userDefaults.
I would like to verify that the data is correctly saved and also modify it during development to check that my app behaves correctly.
I can see the data in ~/Library/Containers//Data/Library/Preferences/.plist.
My problem is that when I delete this file to check that my app will behave correctly, then it is re-created with old data. It contains entries that I used for development testing, and not in use any longer. I am unable to get a clean file again.
Where is the original data saved, and how do I access it?
I've looked in:
- ~/Library/Preferences
- ~/Library/Preferences/By Host
- ~/Library/Caches
- ~/Library/Saved Application State
It is not in one of those locations unless it is named differently. Even a search on my mac did not find it.
Yes, this is currently for a macOS app where I can see the plist files in finder (or terminal).
In a sandboxed app your preferences get stored within your app’s container. For example, for the app with a bundle ID of com.example.apple-samplecode.Test721805
:
-
The container is
~/Library/Containers/com.example.apple-samplecode.Test721805
. -
The preferences file that backs
UserDefaults
is~/Library/Containers/com.example.apple-samplecode.Test721805/Data/Library/Preferences/com.example.apple-samplecode.Test721805.plist
.
Hence:
% plutil -p ~"/Library/Containers/com.example.apple-samplecode.Test721805/Data/Library/Preferences/com.example.apple-samplecode.Test721805.plist"
{
"Blobble" => "Blibble"
}
WARNING The location and format of this file is subject to change. It’s fine to poke around in it for debugging purposes, but do not encode such knowledge in your product. Rather, always access user defaults via the UserDefaults
API.
There is, however, a much better option, namely the defaults
command-line tool. For example:
% defaults read com.example.apple-samplecode.Test721805
{
Blobble = Blibble;
}
For more info about that tool, see the defaults
man page.
and with a TSI that has gone unanswered for two weeks
Hmmmm, that’s not good. Please get in touch with me via email (my address is in my signature). Make sure to reference this thread.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"