Comparing Version Numbers in Objective-C – GitHub

I often have need to compare version numbers in my apps. One particular example is for keeping an online version of a database up to date within the app.
In this case I have a php file that outputs just the latest version number, I can then compare this with the version number stored in the app. If the app has a lesser version, download the update, if not, leave it as it is.

I thought there’d be some sort of built in function for comparing version number but there wasn’t. So I set about defining a solution for myself.

Intoducing the VersionComparator

I didn’t want to do what some previous solutions where doing and just return true or false based on a set of version numbers. I wanted to somehow read how out of date the current number was in comparison to the online version, so in theory, my user could decide for themselves whether the update is ‘worth it’.
For example, in a work-use situation where you need to use the app quickly and move on, a small build change where some spelling errors have been corrected wouldn’t be important and the user could choose to ignore this update, but one where a large amount has been changed may be more worth adjusting.

The best way I could figure to do this would be to assign some sort of number to the version number that represents it’s magnitude and compare between these numbers. There is more capability here than just seeing which is higher, because now, you can see an actual integer representation of how much higher the online version is than the current.

Here’s how it works

Download the VersionComparator from GitHub and add the two files to your project.

Next, import it at the top of your code:

Now you can compare version numbers from within the class:

By getting a real number value representation of the version number, you can make calculations based up it!

Hope you get some use out of it.
Let me know in the comments.

Comments

comments

Powered by Facebook Comments

This entry was posted in Labs.