Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - comparing app versions

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.

like image 638
bobsacameno Avatar asked Dec 16 '14 12:12

bobsacameno


1 Answers

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)
like image 87
Clafou Avatar answered Sep 18 '22 22:09

Clafou