Search code examples
iosinstallationupgradereinstall

How to detect an iOS App installed or upgraded?


I am developing an application and i need to know whether user installed the app for the first time or upgraded it from the App Store.

How can i detect whether app is installed for the first time or upgraded or re-installed?

Thanks for your answers in advance.


Solution

  • You can differentiate between the first start after installing the App, the first start after an update and other starts quite easily via saving the latest known version to standardUserDefaults. But as far as I know it is not possible do detect a re-install of the App as all App-related data are also removed when the App is deleted from the device.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        NSString* currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
        NSString* versionOfLastRun = [[NSUserDefaults standardUserDefaults] objectForKey:@"VersionOfLastRun"];
    
        if (versionOfLastRun == nil) {
            // First start after installing the app
        } else if (![versionOfLastRun isEqual:currentVersion]) {
            // App was updated since last run
        } else {
            // nothing changed 
        }
    
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"VersionOfLastRun"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }