9

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?

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
nOOb iOS
  • 1,384
  • 2
  • 19
  • 37

5 Answers5

6

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;
}
Community
  • 1
  • 1
clearlight
  • 12,255
  • 11
  • 57
  • 75
3

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.

Javier Flores Font
  • 2,075
  • 15
  • 13
3

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"];
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
2

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 ;
}
}
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
  • @JintendraGandhi - thanks for this example. I ported it to Swift and included it in my answer attributing the original to you. – clearlight Mar 27 '15 at 16:29
  • @JintendraGandhi I did not ask the question, so I cannot (am not enabled to) accept it. I can only up vote your answer (which I will now). I didn't need the solution I only felt it would benefit Swift users to see it ported to Swift, which I know well enough to port it from looking at your code, as it is a small straightforward function. My answer was already up voted, so I'm just doing this as a service for future viewers of the thread. – clearlight Mar 27 '15 at 16:32
  • Oops.. nyways.. Thnxx :) – itsji10dra Mar 27 '15 at 16:33
1

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;
}
Bhupendra Singh
  • 121
  • 1
  • 6