OS X: NSUserDefaults not saved - macos

I have a sandboxed app for OSX and I saved some data in NSUserDefaults, everything was fine until I deleted the plist file from the ~Library/Preferences/ directory.
I thought the app should recreat it but it did not. When I debug I see that
[[NSUserDefaults standardUserDefaults] synchronize] method returns YES
and data are saved in the class but when I restart the app user defaults is empty.
Even if I copy those plist file from backup to the Preferences directory, the app does not see it.
Of course the plist should be saved in the Containers/my app bundle id/Data/Library/Preferences and when I copy the plist file from backup to that directory, the app can see it, but why that file is not recreated when I deleted it?
Does anybody know why is that?

Check this question - might be related to caching or prefs location when running in the sandbox:
Mac sandbox created but no NSUserDefaults plist

I had this problem, which is caused by deleting the files manually. What you can do instead is to use the defaults terminal command. This way I was able to reset the defaults without breaking things. Replace "bundle-identifier" with your bundle id.
To view the current values:
defaults read bundle-identifier
To remove stored values:
defaults delete bundle-identifier

Related

How to clear UserDefaults for Xcode build of Mac app?

I'm trying to make sure my first-run code works properly, and so I'd like to clear the preferences file created by UserDefaults.standard.set calls. Where is it located, or is there at least a way to clear it out (other than writing it into my app)?
Running Product > Clean does not clear out the defaults.
I've looked in DerivedData, ~/Library/Preferences, /Library/Preferences, and haven't found what I'm looking for.
If the app is sandboxed the preferences are located in
~/Library/Containers/[bundle-identifier]/Data/Library/Preferences
If it's not sandboxed the preferences are at the usual location
~/Library/Preferences
You can use defaults command In Terminal
$ defaults delete com.bundle.identifier
Also you can delete any value in defaults by key, if you don't want to delete whole application defaults plist.
$ defaults delete com.bundle.identifier kSomeKey

Where are the application preference files location on Mac

I am having a hard time finding from where my application is picking up its window dock and location settings. I removed the related plist files and folders from the following directories:
[USER]/Library/Preferences
[USER]/Library/Preferences/By Host
[USER]/Library/Caches
[USER]/Library/Saved Application State
But the old window settings are retained when I launch the application. The application is using CFPreferencesCopyValue method to read preference values:
::CFPreferencesCopyValue("Toolbars:MyTools:Application", "kCFPreferencesCurrentApplication", kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
I am not very familiar with Mac's preferences mechanism. Can someone explain what could be happening here? Thanks
This problem hit me recently so I thought I'd post the answer here belatedly.
When you delete the preferences on a Mac (plist file) make sure you clear the cached preferences otherwise Java programs can keep working with the cached settings.
You can:
after you have deleted your plist file
killall -u <your-user-name> cfprefsd
OR
reboot
Items 2. and 3. will cause the cache to be cleared and then your preferences will be reloaded as the cfprefsd restarts automatically.
I hope that helps.
The NSUserDefaults on Mavericks (at least) are cached and it is not recommended to edit the plist files manually. The actual files reside in a container folder (you may know this folder from sandboxing).
But you can use the command line utility defaultsto edit, change or delete preferences. To delete the defaults (= reset defaults for your app) you can run in terminal:
defaults delete com.myapp.* && rm -rf ~/Library/Preferences/com.myapp.*
This is taken from a blog entry that shows in detail explanations on user defaults and mavericks.

NSSavePanel for saving a file after sandboxing

I have a mac AVRecording app, which records a video and save it to a location selected via NSSavePanel. It was working fine till I sandboxed the app.
For sandboxing I have added the following entitlements
com.apple.security.files.user-selected.read-write
com.apple.security.assets.movies.read-write
com.apple.security.files.downloads.read-write
This enables saving to Downloads and movies folder only.
How is it possible to save my file to any desired location, Desktop, Documents etc ?
It's not clear from your question whether you are referring to saving a particular file (in which case you can use the NSSavePanel and manually copy the file using NSFileManager to write the file into the user-specified new file), or whether you are referring to having the user choose a location for all future downloads.
If you want to prompt the user for a location to use for future downloads, you'll need to use the secure bookmark entitlement and secure bookmarks to retain access to the folder.
There's another stackoverflow answer about sandboxing which covers the process of saving and using the secure bookmark.

MacOS X: Remove plist file when the app is removed

I am showing a license form at app startup, but it should be shown only at the first time the app launched. Not always. So, i tried to store a flag in plist or user defaults, but upon the app removing(move to trash), plist or userdefaults is not getting removed from system, thus i'm not able to achieve my task.
This is what i have been trying below,
I am storing a flag in plist file and save the file(in Documents directory) during App launch, so next time , i can read from there and find out whether the app is running first time or not. I am failing here, because even the app is removed(move to trash) from system, i am not able to remove this plist file.
I tried to set a flag in NSUserDefaults too, but after deleting the app and build again and launch the app, it still persists the previous value i stored. I am wondering, NSUserDefaults doesn't get deleted when app is deleted from system?
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:#"1" forKey:#"APPLAUNCHED"]; // store user default
[ud synchronize];
Could someone please advise?
Thank you.
Neither user defaults nor the application sandbox get automatically removed when the application is deleted. I think there's no way to do that.

Updating an iphone app with plist

I am a question about plist. If i have already released an app say version 1.0, which contain a plist, allows user to save their data in. If i now release a version 2.0, will the plist data entered by the user in the previous version being deleted.
I have tried on my phone, by using build and run from the xcode seems it doesn;t delete the pervious. But i am not sure will it be the same in itunes store.
Thank you.
Since you're saying that user's save their data in the plist, I will assume you are saving that plist to the documents directory. If that is the case, I don't see why it will be an issue as the directory is persistent between app updates. And if you are providing a new plist file with this update then it won't overwrite that plist file unless you instruct it to as the new plist file will be inside the bundle. You might want to check whether a plist exists already in the documents directory. If it exists, perform some kind of merge operation between both the plist files.

Resources