How To Get iOS Version In Swift

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 and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts