OS X Lion: How to store persistent data under Application folder - osx-lion

I need to store a persistent data for my Mac application. I'm able to store this data in a plist or NSUserdefaults. But I want to store this persistent data in my application folder, so that when I delete the app (move to trash) from the Mac, this persistent data also will get deleted. I want this to work like that. What would be best approach to do this and how can I store persistent data in my app folder, so that the data also gets deleted when the app is deleted?
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:#"1" forKey:#"APPLAUNCHED"]; // store user default
[ud synchronize];

It is not a good practice to store persistent data in the applications folder. You should use the
Home or Application Support Directory depending on the type of content you want to store
The following describes the Mac Application guidelines
http://developer.apple.com/library/mac/#documentation/General/Conceptual/MOSXAppProgrammingGuide/AppRuntime/AppRuntime.html

Related

Will App Store Update Erase Core Data/NSUserDfeaults?

My app uses Core Data to store multiple entries, and NSUserDefaults to store a value. If I publish an update to the app store, will it erase that info? I have not adjusted the code for the Core Data; however, I added code for a nil/non-existent case for the NSUserDefaults
No, updating an app does not erase the app's data, even if the app uses Core Data or NSUserDefaults. Deleting and reinstalling an app removes app data stored on the device, but doesn't touch any cloud data the app may have stored.

How to create a setup in xcode/iPhone SDK

I'm wondering how I can add some kind of setup to my App so when the user launches the App it asks a couple of questions and based on those questions it redirects to a specific view and the next time the app launches it will continue there again.
Is there any way to achieve this, because I'm unsure where I should start searching for.
I did something similar in my last project. There the user had the option to use the application connected with facebook or not. For storing the decision of the user I used a key/value pair in the NSUserDefaults. It's very easy to read/write and its persistently stored in the apps filesystem.
I first checked the NSUserDefaults if the key/value pair already exists.
If not, I did a redirection to a ViewController containing the two buttons (With FB/No FB)
Then depending on the selection of the user I setted the NSUserDefault key.
If yes, I did read the NSUserDefaults and get the value of the key.
Depending on the loaded value I redirected him to the FB ViewController or the the normal one.
Here a short example of reading the NSUserDefaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:#"keyToLookupString"];
Here a short example of writing the NSUserDefaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"TextToSave" forKey:#"keyToLookupString"];
I guess the only difference to your application is that you have several values to store but for that you can still use the NSUserDefaults. You could also use the CoreData to store the information but I think in that case using the NSUserDefaults is the way to go.

Backing up NSUserDefaults and synching iPhone

Is the application domain in [NSUserDefaults standardUserDefaults] backed up when the user synchs their device? If not, can you suggest a close correct alternative?
Apple makes reference to "Application Preferences" in its documentation, such as regards in-app purchasing. I understand, perhaps incorrectly, that they're making reference to NSUserDefaults here although the terminology doesn't appear to match perfectly.
In-app purchases, which I plan to record in [NSUserDefaults standardUserDefaults], need to be backed up in my project.
Thanking you kindly in advance.
Yes. NSUserDefaults uses a PLIST file as a backing store, which is backed up on each sync. See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/UserDefaults/Concepts/DefaultsDomains.html for more information.
If you wanted to see for yourself, you could check out ~/Library/Application Support/MobileSync/Backup/. Create an unencrypted backup of a device with just your app on it, and view the files in the PLIST editor.

How persistent is [NSUserDefaults standardUserDefaults]?

I'm using [NSUserDefaults standardUserDefaults] for storing application settings.
My questions are:
do those settings are removed on app deletion?
are they kept after an application update (through the
AppStore)?
Because I'm using it to store a password and don't want my users to reset them at each update. Also, I'd like that the only way to reset the password would be to remove the app and re-install it.
Is NSUserDefault the right choice?
Thanks,
Jérémy
Yes, they are removed on app deletion and yes they are kept when an application is updated.
However, you're not advised to store sensitive data in the NSUserDefaults, instead I would look at using the Keychain.
I use NSUserDefaults in my app to allow additional access to my app for my colleagues. They just have to enter the code word in settings and the app is fully opened.
to the point each time I update the app they have to re-enter the code word, so I would say from experience that they are not kept after updates. The values need to be re-entered.

How do I save my iPhone application data without a server?

Is there any way to save data without connecting to server on the iPhone? If so, please describe how.
Look at the NSUserDefaults class. It can save primitives and NSArray objects of primitives.
Example, your app might write out before app terminates in your app delegate:
[[NSUserDefaults standardUserDefaults] setString:#"Value" forKey:#"Key"];
And then read it in after launch:
NSString *dataYouNeed = [[NSUserDefaults standardUserDefaults] stringForKey:#"Key"];
Just remember to handle the nil case gracefully (i.e. that the app will keep working immediately after a fresh install or a reset to the user defaults).
Edit: the question has already been more-or-less rewritten. Obviously if you need to store data that will act as your application's model (in the MVC sense), Core Data or sqlite is where you would do best to start looking.
If it's a small number of things, use NSUserDefaults. If it's a lot of stuff, use Core Data.