Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if user has updated app or installed a fresh copy?

I will be sending out an update to my app with a new data structure, therefore if a user is updating my app I need to update their current data. So I was wondering how can I programatically tell if the user updated my app or installed a new copy (if a new copy is installed I don't need to update anything) ?

like image 780
Christian Gossain Avatar asked May 17 '11 17:05

Christian Gossain


2 Answers

Checking the data structure is a solid solution. I began to worry in my own apps about folks who don't upgrade for several versions. I felt this would lead to a myriad of structure checks. The code I show below determines and stores the version and previous version in the NSUserDefaults. You could code for those varying version difference scenarios if needed.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
BOOL versionUpgraded;
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *preVersion = [prefs stringForKey:@"appVersion"];

if ([prefs stringForKey:@"appVersion"] != nil) {
    //see if version is the same as prior
    //if not it is an Upgraded
    versionUpgraded = !([preVersion isEqualToString: version]);
} else {
    //nil means new install
            //This needs to be YES for the case that
            //"appVersion" is not set anywhere else.
    versionUpgraded = YES; 
}

if (versionUpgraded) {
    [prefs setObject:version forKey:@"appVersion"];
    [prefs setObject:preVersion forKey:@"prevAppVersion"];

    [prefs synchronize];
}
like image 134
dredful Avatar answered Oct 10 '22 00:10

dredful


That depends on the kind of data structure you're using.

In general, I would advise you against relying on checking your application version: a user using 2.0 might have just upgraded or it might be a new user.

I'd rather check if there's a data structure already, and act accordingly. Assuming that you're using a Sqlite-backed Core Data storage, you can either check whether the .sqlite file exists, or check if there are objects in your storage.

like image 36
magma Avatar answered Oct 10 '22 01:10

magma