nOOb iOS
nOOb iOS

Reputation: 1394

Find out the first time opening the app when updated to newer version

The app's current behavior is, the user logged in once will not be logged out unless the user explicitly clicks on the logout.

I keep the user logged in, even if the user closes the app and opens it again.

When newer version of my app is released in appstore, I want to find out whether the user updated my app and opened it for the first time.

At that point I want to make them login again.

Is there a way to find out at the first time launch of the app after its been updated to latest version?

Upvotes: 9

Views: 2815

Answers (5)

Daniel Storm
Daniel Storm

Reputation: 18878

Use NSUserDefaults to store the CFBundleVersion. Then check against it every time the application is launched.

// Check if new version
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *currentAppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
if ([defaults objectForKey:@"savedAppVersionKey"] != nil) {
    // Key exists
    NSString *savedAppVersion = [defaults objectForKey:@"savedAppVersionKey"];
    if ([currentAppVersion isEqualToString:savedAppVersion]) {
        // Still running the same app version
        // Do nothing
        NSLog(@"App version: SAME");
    }
    else {
        // The app version changed from the last launch
        // Do something here
        NSLog(@"App version: UPDATED");
    }
}
// Set the key & synchronize
[defaults setObject:currentAppVersion forKey:@"savedAppVersionKey"];

Upvotes: 4

clearlight
clearlight

Reputation: 12615

Create some kind of version #'s scheme. Note: You can enable Xcode to create backups and versions whenever you make substantial changes to the code.

There are a number of ways one could create a version constant, save it, and read it back.

When you update an app from the store, there is app data that persists from the previous installed version of the app, which you can read back to determine the version and, then update that persistent data to be ready for the next update cycle.

This answer was a very popular solution in another similar question.

Or, try something like @JitendraGandhi's ObjC answer below, or if you use Swift, try something like my port of @JitendraGandhi's ObjC example to Swift:

func hasAppBeenUpdatedSinceLastRun() -> Bool {
    var bundleInfo = Bundle.main.infoDictionary!
    if let currentVersion = bundleInfo["CFBundleShortVersionString"] as? String {
        let userDefaults = UserDefaults.standard

        if userDefaults.string(forKey: "currentVersion") == (currentVersion) {
            return false
        }
        userDefaults.set(currentVersion, forKey: "currentVersion")
        userDefaults.synchronize()
        return true
    } 
    return false;
}

Upvotes: 5

Bhupendra Singh
Bhupendra Singh

Reputation: 121

Following code will return NO / YES. You can call this method multiple times to know whether app was updated before this launch or not.

- (BOOL)launchedFirstTimeAfterUpdate
{
    static NSString *lastVersion;
    NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *versionKeyName = @"lastLaunchedVersion";
        lastVersion = [[NSUserDefaults standardUserDefaults] stringForKey:versionKeyName];
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:versionKeyName];
        [[NSUserDefaults standardUserDefaults] synchronize];
    });

    if (!lastVersion.length)
    {
        // No last version means, launched first time
        return NO;
    }

    if ([lastVersion compare:currentVersion options:NSNumericSearch] == NSOrderedAscending)
    {
        // Last version is less than current version
        return YES;
    }

    return NO;
}

Upvotes: 1

itsji10dra
itsji10dra

Reputation: 4675

If you want simple and easy solution, Use this function :

-(BOOL)isAppUpdated
{
NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [bundleInfo objectForKey:@"CFBundleShortVersionString"];

if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"currentVersion"] isEqualToString:currentVersion])
{
    return NO ;
}
else
{
    [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"currentVersion"];
    return YES ;
}
}

Upvotes: 2

Javier Flores Font
Javier Flores Font

Reputation: 2093

You can save your currentversion to NSUserDefaults and use this method to check your version every time the app awakes:

#pragma mark - NSBundle Strings
- (NSString *)currentVersion
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

if the currentversion is different from stored... its time to show the login! Hope it helps you.

Upvotes: 3

Related Questions