Where is a Mac Application's NSUserDefaults Data Stored?

后端 未结 5 1674
梦如初夏
梦如初夏 2020-12-04 07:00

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

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@\"dummy stri         


        
相关标签:
5条回答
  • 2020-12-04 07:35

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

    0 讨论(0)
  • 2020-12-04 07:44

    (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

    0 讨论(0)
  • 2020-12-04 07:46

    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.

    0 讨论(0)
  • 2020-12-04 07:52

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

    0 讨论(0)
  • 2020-12-04 07:56

    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
    
    0 讨论(0)
提交回复
热议问题