How to check iOS version?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to check iOS version?

How to Check iOS Version like a Pro! 📱

So you want to know how to check the iOS version on your device? Well, you've come to the right place! Whether you're a tech newbie or a seasoned pro, we've got you covered.

The Challenge 🤔

One of our readers recently asked us this tricky question:

"How can I check if the iOS version of my device is greater than 3.1.3? I've tried using [[UIDevice currentDevice].systemVersion floatValue], but it's not working. I just want a simple if statement to do the job. Can you help?"

The Solution 💡

Don't worry, we've got a super simple solution for you! Instead of using floatValue and comparing floating-point numbers, we'll make use of the NSDecimalNumber class to get accurate results. Here's how you can achieve it:

// Get the current iOS version as a string
NSString *currentVersion = [[UIDevice currentDevice] systemVersion];

// Create an NSDecimalNumber with the desired version
NSDecimalNumber *desiredVersion = [NSDecimalNumber decimalNumberWithString:@"3.1.3"];

// Create an NSDecimalNumber with the current version
NSDecimalNumber *currentVersionNumber = [NSDecimalNumber decimalNumberWithString:currentVersion];

// Compare the two versions
NSComparisonResult result = [currentVersionNumber compare:desiredVersion];

// Check the result
if (result == NSOrderedDescending) {
    // Current version is greater than 3.1.3
    // Your code here!
} else if (result == NSOrderedAscending) {
    // Current version is less than 3.1.3
    // Your code here!
} else {
    // Current version is equal to 3.1.3
    // Your code here!
}

Now you have an if statement that compares the iOS version with 3.1.3 in a more accurate way. No more floating-point weirdness!

Going the Extra Mile ✨

If you're looking to take it a step further, you can even create a handy utility method to make this comparison reusable throughout your codebase. Here's an example of how you can do it:

+ (BOOL)isCurrentVersionGreaterThan:(NSString *)version {
    NSDecimalNumber *desiredVersion = [NSDecimalNumber decimalNumberWithString:version];
    NSDecimalNumber *currentVersionNumber = [NSDecimalNumber decimalNumberWithString:[[UIDevice currentDevice] systemVersion]];
    NSComparisonResult result = [currentVersionNumber compare:desiredVersion];
    return (result == NSOrderedDescending);
}

Now, whenever you want to compare the current iOS version, you can simply call this method, like so:

if ([YourUtilityClass isCurrentVersionGreaterThan:@"3.1.3"]) {
    // Current version is greater than 3.1.3
    // Your code here!
} else {
    // Current version is less than or equal to 3.1.3
    // Your code here!
}

Conclusion 🎉

You've made it through! Now you know how to check the iOS version on your device like a pro. We hope this guide has been helpful to you. If you have any further questions or you want to share your own insights, feel free to leave a comment below.

Now it's your turn! Go ahead and implement this code snippet and let us know how it works for you. Happy coding! 👩‍💻👨‍💻

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my