Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

objective c - Check if my IOS application is updated

I need to check when my app launches if it was being updated, because i need to make a view that only appears when the app is firstly installed to appear again after being updated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could save a value (e.g. the current app version number) to NSUserDefaults and check it every time the user starts the app.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ...

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *currentAppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSString *previousVersion = [defaults objectForKey:@"appVersion"];
    if (!previousVersion) {
        // first launch

        // ...

        [defaults setObject:currentAppVersion forKey:@"appVersion"];
        [defaults synchronize];
    } else if ([previousVersion isEqualToString:currentAppVersion]) {
        // same version
    } else {
        // other version

        // ...

        [defaults setObject:currentAppVersion forKey:@"appVersion"];
        [defaults synchronize];
    }



    return YES;
}

The version looks like this:

let defaults = NSUserDefaults.standardUserDefaults()

let currentAppVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let previousVersion = defaults.stringForKey("appVersion")
if previousVersion == nil {
    // first launch
    defaults.setObject(currentAppVersion, forKey: "appVersion")
    defaults.synchronize()
} else if previousVersion == currentAppVersion {
    // same version
} else {
    // other version
    defaults.setObject(currentAppVersion, forKey: "appVersion")
    defaults.synchronize()
}

The version looks like this:

let defaults = UserDefaults.standard

let currentAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let previousVersion = defaults.string(forKey: "appVersion")
if previousVersion == nil {
    // first launch
    defaults.set(currentAppVersion, forKey: "appVersion")
    defaults.synchronize()
} else if previousVersion == currentAppVersion {
    // same version
} else {
    // other version
    defaults.set(currentAppVersion, forKey: "appVersion")
    defaults.synchronize()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...