FabKremer
FabKremer

Reputation: 2179

NSUserDefaults behaviour with app update

I have a method in my iOS app that updates the application when detects when my server has a greater version for my app (a new ipa version). If the user wants to download it, the app updates itself on the iPad.

The thing is that I want to update some entities atributes from the DB when the app opens the new version for the first time, but i'm not sure how to. I can't debug it cause when I download the latest ipa, for XCode the app crashed.

I was thinking about doing something like this in the AppDelegate.m:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
   //do the stuff i wanna do
}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // This is the first launch ever
}

But I don't know if this [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"] was set to YES before the update, cause the process should be:

1)Launch the app for the first time ever. 2)The app detects a newer version. 3)Download the same app -> At this point apple "replace" the older version to the newer one. 4)Open the newer version app. 5)Do the stuff i wanna do ONLY for the first time I launch the new version.

Upvotes: 2

Views: 2371

Answers (2)

hasan
hasan

Reputation: 24195

They will not be removed. And will have old values.

if "HasLaunchedOnce" is used in the old version. use a new one and call it "HasUpdateLaunchedOnce".

Check both values and decide what do you want to do.

if (HasLaunchedOnce exists && HasUpdateLaunchedOnce not exists)
  // proceed

Upvotes: 0

Teddy
Teddy

Reputation: 184

You could use an integer stored in NSUserDefaults with a version number hard-coded for each version of the app. If the integer is lower than the hard-coded version, prompt for updates:

NSInteger currentVersion = 3; // increment with each new version

if ([[NSUSerDefaults standardUserDefaults] integerForKey:@"HasLaunchedForVersion"] < currentVersion) {

    [[NSUserDefaults standardUserDefaults] setInteger:currentVersion forKey:@"HasLaunchedForVersion"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // This is the first launch for this version

} else {
    // App hasn't been updated since last launch
}

Upvotes: 5

Related Questions