SSS
SSS

Reputation: 119

Executing function for lower iOS versions

Is there an way for executing functions only for lower iOS versions. I mean we can execute always using the available attribute for iOS versions greater than the specific one ; but is there any way to do the other way round?

Upvotes: 1

Views: 707

Answers (2)

Feridun Erbaş
Feridun Erbaş

Reputation: 654

It's really annoying to write if statement and then catch what you want in the else statement. Nevertheless you can define global function to handle all cases in one place, which is also have its own issues. Never call it with a variable! Always use hardcoded string version number to check iOS version. If you accidentally checked unhandled version, just insert it and go. fatalError() will help you.

func available(version: String) -> Bool {
switch version {
case "13.5":
    if #available(iOS 13.5, *) { return true }
case "12":
    if #available(iOS 12, *) { return true }
default:
    fatalError("iOS \(version) is not handled. Insert:\n case \"\(version)\": \n if #available(iOS \(version), *) { return true }")
}

return false }

Usage:

if !available(version: "10") {
    print("Not available")
}

Upvotes: 0

Frankenstein
Frankenstein

Reputation: 16341

It's simple. You can use this:

if #available(iOS 10, *) {} else {
    print("Code only for versions below iOS 10")
}

Upvotes: 2

Related Questions