View Full Version : Auto delete App Preference file upon closing App?

Jack Flash
Aug 15, 2007, 01:38 AM
Is there a way to have OS X auto-delete a .plist file when an Application is closed?

Aug 15, 2007, 07:06 AM
I'm not sure why you'd want to do this, but it can be done easily enough.

- If you haven't already done so, you need an object acting as the application's delegate i.e.
[[NSApplication sharedApplication] setDelegate:self];

- Then you need to implement applicationWillTerminate, and put your code in there.

- You could hard code the path of the preference file by looking in ~/Library/Preferences; but the correct way might be to use NSUserDefaults method removePersistentDomainForName (I haven't tried it, so I'm not sure if that deletes the preference file, but I expect it would).

Aug 15, 2007, 07:39 AM
You could use the delegate of [NSApplication sharedApplication] as described above to store the revelent information rather than using preferences, and that is lost when the application closes ;).

PS [NSApplication sharedApplication] can be accessed with the constant NSApp.