Do we need something like '#if available'?

In this specific case, since you're checking for the existence of a new framework, you can do this:

#if canImport(WidgetKit)
  import WidgetKit
#endif

func doStuff() {
  #if canImport(WidgetKit)
    if #available(macOS 11.0, *) {
      // Use macOS 11.0-only code
      WidgetCenter.shared.reloadAllTimelines()
    }
  #endif
}

But this only works because you're using an entirely new framework that didn't exist before. What I don't think is possible today is using a new API in an existing framework while still supporting compiling against the old SDK as well, without introducing your own compilation condition. There's some older discussion about this:

2 Likes