Update:
Now you should use new availability checking introduced with Swift 2:
e.g. To check for iOS 9.0 or later use can this:
if #available(iOS 9.0, *) {
// use UIStackView
} else {
// show sad face emoji
}
or can be used with whole method or class
@available(iOS 9.0, *)
func useStackView() {
// use UIStackView
}
or with guard
guard #available(iOS 14, *) else {
return
}
For more info see this.
UPDATE:
based on Allison's comment I have updated the answer, check is still runtime, but compiler can know in advance & can show better error or suggestion while you are working on it.
Other ways to check:
if you don't want exact version but want to check iOS 9,10 or 11 using if:
let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue
EDIT:
Just found another way to achieve this:
let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1)
let iOS7 = floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_7_1)