How to check platform versions in Swift

Issue #477

Mark APIs availability

1
2
3
4
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public extension View {

}

Check platform

1
2
3
4
5
#if canImport(UIKit)
import UIKit
#elseif canImport(OSX)
import AppKit
#endif

In watchOS app, it still can import UIKit, so for only iOS usage, we need to use os check

1
#if canImport(UIKit) && os(iOS)

Check environment

1
2
3
4
5
#if targetEnvironment(macCatalyst)
print("UIKit running on macOS")
#else
print("Your regular code")
#endif
1
2
3
4
5
#if targetEnvironment(simulator)
// your simulator code
#else
// your real device code
#endif

Comments