MacOS X: Remove plist file when the app is removed - macos

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.

Related

Drag & Drop between Photos and a Sandboxed app

I have a Sandboxed app. I'd like the user to be able to drag a picture from the Photos app into my sandboxed app.
I wrote all the code for handling the "promised file" from Photos no problem when using it with a not sandboxed app. But with my sandboxed app, Photos is telling me it has no right to write the picture file in the destination folder... which is the temporary folder inside my sandboxed app.
That's kind of logical that Photos can't write inside my sandbox, even in the temporary folder. But as my app has no access outside the sandbox, how can I exchange the file from Photos to my App ???
Edit to provide some code as per request
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]] ;
NSError *error = nil ;
[[NSFileManager defaultManager] createDirectoryAtPath:tempPath
withIntermediateDirectories:NO
attributes:nil
error:&error] ;
filenames = [sender namesOfPromisedFilesDroppedAtDestination:[NSURL fileURLWithPath:tempPath]] ;
(filenames is global variable NSArray)
And the error message that Photos is showing (SOrry, the picture is in French, but the message isn't really usefull as it can't be shown completely. The part that is shown says "0 file (out of 1) exported. Impossible to move "image" to "folder" : either this folder does not exist, or the folder can't be //cut//
I of course checked that the folder does exist.
Edit with some more information
I think I found one investigation path :
When I create the unique folder in the temporary folder, it gets a "quarantine" flag. I checked that using the terminal and "ls -l", one can see a "#" after the permissions. Now, If I don't create the unique folder, Photos can drop the file directly in the temporary folder. Therefore the problem is that Photos can't use the "quarantined" folder.
Now this does not help yet. As anyway the drop picture file also has the quarantine flag set, my App can't read it !
I have found no way to get rid of those quarantine flags.
The strangest thing is that everything is working fine if I just step by step with the debugger.
The Sandbox will allow file promise provider application to write into the Document folder.
[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
OK, I found what was wrong. It has nothing to do with quarantine or app being sand-boxed.
In fact, when namesOfPromisedFilesDroppedAtDestination: is called, the provider application (in my case Photos) starts writing the files in the temporary folder. This can take some time. Therefore, I was starting to monitor the events in the destination folder, and was reacting on the first event only. (check this : How can my OS X app accept drag-and-drop of picture files from Photos.app?).
BUT
The Photos app is sending the files in several steps. The first steps create a temporary file, which is later renamed to the "correct" image file. This creation of temporary file was the event that my app captured. My app was then checking for the image file to be present, and it wasn't, yet.
The solution is therefore, to continue checking for folder events until the promised file is really there. If it is not yet, then continue waiting. Hopefully, the promising app will fulfill its promise one day.
Thanks to catlan for pushing me to re-investigate.

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.

OS X: NSUserDefaults not saved

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

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.

Preset NSUserDefaults

I thought I read some where that I could include a NSUserDefaults file with a cocoa app. I cannot seem to find how to do that.
I know I could just create on first app launch but I dont want to do that just yet...
The User Defaults Programming Topics doc is your friend. You'll want to -registerDefaults:, which is going to be an NSDictionary instance created at runtime or loaded from a plist file in your app's resources folder.
As to not wanting to do it just yet, when did you plan to? If you're going to register your app's defaults at all, it's to be done at every launch.

Resources