0

as I get closer to releasing my app, I'm trying to make sure that I'm using stable code to check if the app has been launched before (in order to perform some first time setup). Is this (obviously a no frills method that doesn't take into account app version and updates) pretty much a rock solid way to determine if the app has been launched?

In my app delegate didFinishLaunchingWithOptions method, I perform the following each time:

NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];

if(![defaults objectForKey:@"not_first_launch"])
{
    NSLog(@"This is the first time the app has been launched.\nPerforming first-time setup procedures...");
    [self runFirstTimeSetup];
}

My second question is basically, can I assume that when I release an app update, that the user's documents directory for my specific app's sandbox will be left unerased? Does an app update simply add to the directory, not wipe it clean and re-install? I need the user's files to stick around even when I update the app (pretty obvious) but I don't want to make the wrong assumption and have users lose data every time I release an update.

Thanks!

Rich Byden
  • 509
  • 2
  • 6
  • 13

2 Answers2

2

Yes, that's a good use of NSUserDefaults.

User data is preserved through updates.

Just make sure you keep the data that comes with your app and the data generated during runs in separate bins. So that if you have to change the former (via an app update), the latter remains untouched. For example, don't put both of them in the same SQLite table.

Sanjay Chaudhry
  • 3,181
  • 1
  • 22
  • 31
1

I know you already accepted an answer, but I just wanted to touch on the subject. The way I check if the app is being launched for the first time is like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    ...normal code...

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults objectForKey:@"kFirstUse"]) {
        [defaults setObject:[NSDate date] forKey:@"kFirstUse"];
        [defaults synchronize];
    }
}

The reason why I wanted to post is because while I check if this is the user's first launch, I also save the date of their first launch. For no extra code, I figured why not save this first launch date. I don't use that info at all in my app, but I was thinking maybe in the future I might. In the future, I was thinking about adding in App Purchase, and I would give the stuff away for free to my initial users; so this allows me to be prepared for the future and accomplish my goal at the same time.

And just for completeness, NSUserDefaults do persist between app updates, just make sure not to change the key to any object, otherwise that object won't be found.

Another good thing to add is that line [defaults synchronize]; - that literally makes the app save that data. The app periodically automatically saves the data, but I like to be safe and know that my stuff is saved.

let me know if you have any other questions

Andrew
  • 3,874
  • 5
  • 39
  • 67