amirfl
amirfl

Reputation: 1644

How to check if current version of the app has launched before?

I'd like to show a user agreement / disclaimer the first time my iOS app is launched. Currently, I'm simply doing it with the following code in didFinishLaunchingWithOptions in the AppDelegate:

//Show disclaimer to user, if he never agreed to it yet
 if(![[NSUserDefaults standardUserDefaults] boolForKey:kHasAgreedToDisclaimerKey])
      [self showDisclaimer];

However, I need to show a new disclaimer every time a new version of the app is installed and launch for the first time.

One way I was thinking of solving this problem was by creating a different UserDefaults key for each app version, but that looks like it will leave a lot of junk keys on the user's device.

Is there a better way to solve this?

Upvotes: 0

Views: 146

Answers (3)

Aron C
Aron C

Reputation: 838

Sounds like you have the right idea.

I'd try something like this:

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *storedVersion = [prefs objectForKey:@"AppVersion"];

    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *bundleVersion = [infoDict objectForKey:@"CFBundleVersion"];

    if (storedVersion) {
        if (![storedVersion isEqualToString:bundleVersion]) {
            [prefs setObject:bundleVersion forKey:@"AppVersion"];
            [prefs synchronize];

            // Show disclaimer
        }
    } else {
        [prefs setObject:bundleVersion forKey:@"AppVersion"];
        [prefs synchronize]; 
   }

Basically, you are going to copy the bundle version to userDefaults, and compare it on every launch. If it isn't present in defaults, then you know the app was just installed, and you want to display the disclaimer. If it is in the defaults, then you want to check it against the bundle version. Anytime the bundle version doesn't match, you know the app has been updated.

Edit: Looks like took too long to mock up my example. I'm ten minutes too late, and you've found an answer. But I'll leave it here anyways :).

Upvotes: 1

dogsgod
dogsgod

Reputation: 6387

NSUserdefaults will be deleted when the app gets deleted, though data will persist on update. If that is OK for you, you could save a version number of the accepted version there, as mentioned by @Nick in the comment.
To make your information really persistant, write a version key to the keychain when the user accepted.

Upvotes: 0

rebello95
rebello95

Reputation: 8576

Close. Don't save something for each app version, just store the last opened app version and compare it to the current app version. Something like this:

//Get the last opened and current versions
NSString *lastOpenedV = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastOpenedVersion"];
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

//Show an alert if necessary
if (lastOpenedV == nil || ![lastOpenedV isEqualToString:currentVersion]) {

    [self showDisclaimer];

    //Update the last opened version
    [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"LastOpenedVersion"];
}

Upvotes: 4

Related Questions