How To Get iOS Version In Swift

By Eddy Chung

To get the current devices (or simulators) iOS version you can use the following code:

var systemVersion = UIDevice.current.systemVersion
print(systemVersion)
// 13.3

Restrict Code To Certain iOS Versions

If you want to only run code if it meets a certain iOS version, you can use the #available syntax.

if #available(iOS 13.0, *) {
    // use iOS 13 feature
} else {
    // use workaround
}

Check If Feature Is Available Instead Of iOS Version

Instead of checking the iOS version, you can check if it respondsToSelector. This allows you to check specific methods, not relying on the iOS version.

if (self.respondsToSelector(Selector("showViewController"))) {
    self.showViewController(vc, sender: self)
} else {
    // alternative solution
}
If you liked this post, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts