Running Turtle Running Turtle - 4 months ago 13
iOS Question

How to conform to a protocol based on OS version ?

In an app targeting iOS 8.0 and above, I want to use an SFSafariViewController on iOS 9.0 and above and a fallback WebView controller on older devices.

I need those View controllers to conform to a protocol

protocol SomeWebViewProtocol


I can do it directly for the fallback WebView controller (
extension FallBackViewController: SomeWebViewProtocol { ... }
), but how do I do it for the SFSafariViewController ?
if #available(iOS 9, *) { ... }
is not an option since extensions must be at file level

Answer

The following example should suffice:

struct Foo
{

}

@available(iOS 9, *)
protocol Bar
{

}

@available(iOS 9, *)
extension Foo: Bar
{

}

Simply add @available(iOS <version>, *) to the top of your declaration.