Neil Macy

Preparing for future minimum OS versions

Let's say you have an app that supports iOS 13 and above. There will be some code that you can't use until iOS 14 is your minimum supported version. For example, using UIActions with UIButton is only available on iOS 14 and up; in iOS 13 you need to use the old target/action approach.

To handle this, you might have alternative paths in your code. For example:

if #available(iOS 14.0, *) {
    let button = UIButton(primaryAction: { _ in buttonPressed() })
} else {
    let button = UIButton(type: .sytem)
    button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
}

When you update to using iOS 14 as your minimum OS version, you can delete the code in the else block, and delete the version check.

Marking your own code as deprecated

Writing code for the old and the new API can become complicated. Sometimes, it's just better to only use the old API, until your minimum OS version goes up.

When you update your minimum version, you often get warnings from Apple's APIs stating that code has been deprecated. You can use the same approach, to remind yourself that you can use the new API now.

To get a warning when you update the minimum OS version, you can add the @available annotation to your property, method or type:

@available(iOS, deprecated: 14.0, message: "Replace this method with the new UIAction API.")
func createAButtonUsingTargetAction() -> UIButton {
    let button = UIButton(type: .sytem)
	button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
	return button
}

This creates a warning when you compile with iOS 14 as your minimum OS version. That means you get a reminder to go back and update the old code, now that you can use the new API instead.

Published on 16 March 2022