Use New Awesome iOS Features While Maintaining Backward Compatibility

Federico Zanetello
Nov 29, 2016 · 3 min read

Hi! This post has moved to a new blog! Come to for the latest articles!

Every year at WWDC Apple blesses us with plenty of new awesome tools that we can’t wait to implement in our apps.

Image for post
Image for post

However, most of the time, our apps must support previous iOS versions, too.

This might seem a big showstopper and, in fact, there are many developers that are holding on in adding the latest new features because of this.

Well, I’m here to tell you that, thanks to Swift, it is incredibly clean and easy to add new iOS features, while maintaining backward compatibility!


Let’s use a real example:
one of my new favorite toys from this year WWDC is UIPreviewInteraction.

Basically, this (iOS 10.0+) class listens to the very same touch interactions of Peek and Pop for your UIView, but gives you complete control on what to do with it.

To implement it, we must follow just two steps:

  1. Add a UIPreviewInteraction property to our UIView
  2. Create a UIPreviewInteractionDelegate

1. Adding a UIPreviewInteraction Property to our UIView

Obviously, if we add the property as it is, the compiler will complain:

How do we overcome this?
Easy: we don’t tell the compiler what the property is.

Furthermore, since we use the property only in iOS 10 and above, we will make it an optional, so the property will be nil for previous iOS versions:

Ok, now we can compile! 🎉
However, at some point, we must declare that the property is a UIPreviewInteraction.


Simple, we use one of the most precious Swift 2 gifts: #available, a.k.a. Swift’s Automatic Operating System Availability Checking API:

Great! Now our property will be:

  • nil in any iOS prior to 10;
  • an instance of UIPreviewInteraction for iOS 10.0+!

2. Create a UIPreviewInteractionDelegate

For simplicity’s sake, I will use the same UIView class as my delegate as well.

One more time, if we simply try to make our UIView class conform to the UIPreviewInteractionDelegate, the compiler will complain:

And, one more time, Swift 2’s Automatic Operating System Availability Checking API comes to the rescue!

Now, let’s not forget to add the view as its own delegate in our initializer…and we’re done!


Even though our apps has to support older iOS versions, this must not stop us to implement the latest and greatest iOS features!

It’s true, this solution won’t add the same functionality to previous iOS releases, but it’s still way better than waiting years to drop support of older iOS versions to finally implement what’s available to us today.

Code Snippet

You can find the complete code of this post here.

Federico is a Bangkok-based Software Engineer with a strong passion for Swift, Minimalism, Design, and iOS Development.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Swiftly Swift

Write code swiftly

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store