Where is a Mac Application's NSUserDefaults Data Stored?
Asked Answered
C

5

84

I am using NSUserDefaults to store some data in my application.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"dummy string" forKey:@"lastValue"];
[prefs synchronize];

For testing purposes I need to see the System Preferences plist file where my NSUserDefaults data is saving on the Mac.

I know where the iOS application user defaults are stored, but I don't know about mac application. Where is a Mac Application's NSUserDefaults Data Stored?

Carpet answered 2/11, 2011 at 15:15 Comment(0)
B
192

They can be found in more than one place:

~/Library/Preferences/com.example.myapp.plist
~/Library/SyncedPreferences/com.example.myapp.plist

and if sandboxed

~/Library/Containers/com.example.myapp/Data/Library/Preferences/com.example.myapp.plist
~/Library/Containers/com.example.myapp/Data/Library/SyncedPreferences/com.example.myapp.plist
Banister answered 28/9, 2012 at 10:10 Comment(8)
These Plists should never be read or edited directly. They are not necessarily what the NSUserDefaults class reads or writes to. Instead, always use defaults read and defaults write in your terminal. Full explanation at the bottom of this thread: devforums.apple.com/message/894120Juncture
What about for a screensaver?Midtown
I read @TravisB's warning too late and deleted my app's plist file. I was able to restart my computer and rebuild my app to get the file back.Lonnylonslesaunier
@JuanjoConti The "Flurry" screen saver has them in ~/Library/Preferences/ByHost/com.apple.Flurry.<HARDWARE-UUID>.plistKinata
To add to @TBlank's answer, if you're trying to just delete an application's user defaults, you shouldn't do it via deleting the plist file itself - my own testing has shown that the elements will remain cached. To delete, follow the advice in superuser.com/questions/907798/…Boardwalk
hmm - my app is sandboxed -but my NSUserdefaults are not appearing in the containers folder. Do I need to change a setting when saving them?Heimlich
What happens to that data when the user deletes the app?Serpigo
@Serpigo It is cached and still there if you install the app again, before you restart macOS. Cache is cleared after restarting macOS. And if you install the app again after macOS restart, only then you get a clear user defaults for the app.Banister
G
20

In ~/Library/Preferences/com.example.myapp.plist.

Garges answered 2/11, 2011 at 15:28 Comment(1)
@ahmadbaig: this is no longer completely accurate with the advent of sandboxing, you should change the accept answer to erkanyildiz'sTransfer
A
7

(Xcode 7.3.1,macOS 10.11.6)

For Additional, if you are using App Groups

if let prefs = NSUserDefaults(suiteName: "group.groupApps")  {
    ...
}

plist file will be here:

~/Library/Group Containers/group.groupApps/Library/Preferences/group.groupApps.plist
Anastrophe answered 21/7, 2016 at 6:26 Comment(0)
T
2

On Sierra, I found the data here: ~/Library/Application Support/.

Topliffe answered 8/12, 2016 at 17:4 Comment(0)
G
1

One more possible location for these data comes into play when trying things out in a Playground. I was experimenting with UserDefaults in a Playground, using XCode 8.3 and Swift 3, and wanted to see the resulting plist file. After some detective work (UserDefaults files have the bundle identifier in the filename and calling Bundle.main.bundleIdentifier in a Playground gives the XCode identifier) I found to my great surprise that the UserDefaults data was added to:

~/Library/Preferences/com.apple.dt.Xcode

In other words, keys and values are added to the XCode preferences file! I double-checked by coming up with very unlikely strings for the keys and they were indeed added there. I did not have the courage to try using some keys that were already in use by XCode but caution seems good here.

Gegenschein answered 6/8, 2017 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.