I want to update my app on the App Store. When the app is first opened after the update, I want it to sync some stuff. Therefore I need a way to see if it's the first launch after the update.
The solution I thought of is: storing the app version in the NSUserDefaults
like this:
NSString *oldVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"appVersion"];
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"appVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];
now I have the oldVersion
and the currentVersion
and all I need to do is to compare them. I want to know if the oldVersion
is smaller the currentVersion
. But they are strings. how can I check if oldVersion < currentVersion
?
I know I can just check if they are not equal. But I want to be prepared for future updates. Because maybe the syncing I want to perform for this 2 will be different for version 3 and so on.
You can compare numeric version numbers using natural sort order (which will consider 1.10 to come after 1.1, unlike lexicographic sort order) as follows:
BOOL isNewer = ([currentVersion compare:oldVersion options:NSNumericSearch] == NSOrderedDescending)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With