How can I check whether an application has already been installed or is being installed for the first time

前端 未结 1 559
情书的邮戳
情书的邮戳 2021-02-06 19:04

What will be the best way to check that application is already installed or being installed for the first time.

1条回答
  •  醉酒成梦
    2021-02-06 19:48

    Bundle version and saving it in user defaults.

    EDIT:

    There are three things to note here.

    1. Bundle version: This is the version of the your application that you want to release.

    2. Old version: This will indicate previous version of your application. We will store this in user defaults so that we will know what was the old version when updating our application. This will obviously be nil if your bundle version is 1.0.

    3. Target version: This indicates the version the user is targeting. We will discuss this later.

    So, condition such as

    bundleVersion > oldVersion or

    if(isVersionBetter:myBundleVersion thanVersion:oldVersion)
    

    would either mean we want to create our database (in this case bundle version would be 1.0 and old version will be nil) or update our database (in this case bundle version would be something greater than 1.0 and hence old version would not be nil).

    Thus, as we can see, creation of database means user is installing app for the first time. Updating database means user has already installed the app and is updating the database.

    But, there might also be a case when you want to update your app and want to keep the database as it is. That is, only UI updating.

    Here, target version comes into picture.

    As mentioned above, target version is the version the user is targeting. All would work same as above if user is targeting the bundle version. But if user is targeting some other version than bundle version, we would skip database updating part, thus allowing only the UI to change.

    So, the final statement would be something like this:

    if( bundleVersion == targetVersion AND bundleVersion > oldVersion ) {
    // Either create or update the database.
    }else {
    // Do nothing. Skips database updating and allows UI update.
    }
    

    Thus, your database function would look something like this

    -(void) initWithTargetVersion:(NSString *) targetVersion {
    
        NSString *oldDatabaseVersion = [[NSUserDefaults standardUserDefaults] stringForKey:@"OldDatabaseVersion"];
        NSString *bundleDatabaseVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    
        if([bundleDatabaseVersion isEqualToString:targetVersion] && [self isVersionBetter:oldDatabaseVersion new:targetVersion]) {
            // Create or update the database.
        }else {
            // Do nothing.
        }
    }
    

    where user would pass the target version as follows:

    [[DatabaseManager sharedManager] initWithTargetVersion:@"1.0"];
    

    0 讨论(0)
提交回复
热议问题