How to clear UserDefaults for Xcode build of Mac app? - xcode

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

Related

Resetting mac application in xcode

How can I reset a cocoa application in Xcode? When I run my application from Xcode all user default are saved. I would like to reset (delete) my application to create a new one with new settings and run as it would be run first time.
I found one way - I change a name of my app to a different one and it is started as new. But how can I do this with an old name?
You can use the defaults command line tool to remove all the settings, you just need the bundle id of your app (which you can find in your Info.plist file), e.g. if your bundle id is "com.foo.barApp" then you can run this from a terminal shell:
defaults delete com.foo.barApp
Is your app limited to the App Store? If so, I don't think there's a way to delete your entire application, specifically because of the sandbox restrictions. We'd need more information on how you are planning to distribute it to help.
If you just want to delete your app settings, then clear whatever database you store your app data in - [NSUserDefaults +resetStandardUserDefaults], SQLite, App Library directory, whatever. That is dependent on how/where you are storing user data, there's no one size fits all solution.

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.

Set environment variable for the process before startup

I have the following situation:
I have Mac OS bundle with application which uses some 3rd party dynamic libraries and those libraries depend on some environment variable, let's name it ENV_VAR. I want to set ENV_VAR to some value for my application only because if I set it for the whole system it may breaks some other apps. And it should work transparently to the user i.e. he just run my app from the Application folder by double clicking it. How can I achieve it?
NOTE: dynamic libraries are loaded before main functions starts hence setting this variable in the main doesn't help.
You can add a key "LSEnvironment" to your app bundle's Info.plist. The value can be a dictionary with strings for keys and values and those key-value pairs will be added to the environment when your app is launched by Launch Services (e.g. from the Finder or Dock but not from the Terminal).
<key>LSEnvironment</key>
<dict>
<key>ENV_VAR</key>
<string>value</string>
</dict>
However, in my testing (on Snow Leopard), it was a bit flaky to test, at least when editing the Info.plist of an existing app. Basically, Launch Services caches this part of the app's Info.plist when it first encounters the app and won't necessarily recognize changes on disk. You can sometimes prompt it to reread the Info.plist by, for example, duplicating the app bundle or temporarily moving it to a different folder. Of course, the overkill solution would be to use lsregister to flush and rebuild the cache:
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed
This caching issue won't affect your end users, just you as you tweak the Info.plist. Also, it shouldn't affect you if you make the change in your source Info.plist and then build the app using Xcode.
I am not sure if the following works because I don't have such an app to try. The idea is to set the environment variable from the terminal, then call your application:
ENV_VAR=something open -a YourApplication

Remove Sandboxing

I have another question dealing with app sandboxing. So I need access to the users' home directory and at the same time the app should be able to shut down the Mac. This requires to not using sandboxing.
My problem is that I don't know how to remove sandboxing and being able to submit the app to the Mac App Store. I think that the archives are sandboxed because I had turned it on once..
How to remove sandboxing from the archives properly?
Thanks for your help!
On Xcode 11, you can turn off Sandboxing by removing it from the Signing & Capabilities tab:
If I understand what you are asking correctly, you'll need to remove the entitlements.plist from your project and make sure that the Summary view of your target in Xcode has sandboxing turned off:
As Derek Wade pointed out, you can make an App like GarageBand X (which behaves obnoxiously with third party plugins like Amplitube due to Sandboxing) NOT run in a sandbox by editing the binary itself with a HEX editor like HexFiend. Look for:
<key>com.apple.security.app-sandbox</key>
Immediately following that bit you'll see the true tag, which as suggested I switched to 'fals' (no extra bytes) and now GarageBand will happily interact with third party VST plugins. Huzzah.
I found if you go into the .app package, under Contents/MacOS, there should be a binary file that matches the name of your app. Copy that file to your desktop. Edit the desktop copy of the file with TextEdit. You should find within the file, the text representation (xml) of the Entitlements for the app. Find the Sandbox entitlement flag (usually set to <true/>) and change it to <false/>. You will have to unlock the file when editing. Save the file (located on the desktop). Rename the original file in the .app package (i.e. append .old to the filename). Copy the desktop file back to the .app Package location (you may have to authorize it). This should remove the sandboxing.
You cannot remove Sandbox if the user ran you application via Sandbox.
That's the whole point - don't you think ?

Where is user specified data being stored on MacOS

I install an app .app file to /Applications folder.
Can you please tell me where does the user specified data being stored? How can I do to clean uninstall my application and its user specific data?
Thank you.
This depends on the application, unfortunately. Sometimes it is in ~/Library/ folder, though.
AppTrap should do the trick.
It runs in the background and simply has a preference pane in the System Preferences. Drop the application onto the trash folder and it will ask you whether you want the associated application files to be deleted too.
Just let the user do that. For instance with AppCleaner.
Otherwise, just give a way in your preferences to reset them. No need to delete the plist file.
AppDelete is a free program for uninstalling and cleaning up.
FSEventer is a free program for seeing where programs install themselves.
Most information is stored within /Library/, however from time to time you may find applications which store their data within the contents of the .app package itself, or within a subfolder in /Applications/.
This should give you an idea of where to look.
Where to Put Application Files

Resources