Discussion
Loading...

Discussion

  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
John Siracusa
@siracusa@mastodon.social  ·  activity timestamp last month

The if #available(macOS 26, 😉 { … } checks required to call newer APIs from apps that still run on older OSes are really cumbersome to use in SwiftUI.

This technique from @davedelong makes this much nicer and easier: https://davedelong.com/blog/2021/10/09/simplifying-backwards-compatibility-in-swift/

You might still have to do some manual mapping of enums and constants, but that ugliness is contained, leaving your call sites clean.

Dave DeLong

Simplifying Backwards Compatibility in Swift

Every year as new OS and Swift versions are released, the question comes up over and over again: “how do I use this new thing while also supporting older versions?”. While we have a bunch of “availability” tools at our disposal (and I’ll be using them in this post), they always come across as somewhat cumbersome: we need to do inline checks, or we have conditional logic flow that obfuscates the intent of some of our code, and so on.
  • Copy link
  • Flag this post
  • Block
John Siracusa
@siracusa@mastodon.social replied  ·  activity timestamp last month

Since the dawn of SwiftUI, I've been using the (ill-advised, for multiple reasons) custom `if` view modifier as a last resort, which uses basically the same technique as the @davedelong's "Backport" system (with the same downsides, I imagine).

But you can't put an #available(…) check inside an `if` view modifier, and I've been annoyed by this for years. It turns out I just needed to make a slight change to make this work. 🙏

  • Copy link
  • Flag this comment
  • Block
Josh Hrach
@JoshHrach@iosdev.space replied  ·  activity timestamp last month

@siracusa Can you not use the if @availiable(...) check in the `if` modifier? I have a similar modifier and had no issue there.

Sorry, no caption provided by author
Sorry, no caption provided by author
Sorry, no caption provided by author
  • Copy link
  • Flag this comment
  • Block
John Siracusa
@siracusa@mastodon.social replied  ·  activity timestamp last month

@JoshHrach You can’t use it in .if(…) { } in the “…” spot.

  • Copy link
  • Flag this comment
  • Block
John Siracusa
@siracusa@mastodon.social replied  ·  activity timestamp last month

If you’re an iOS dev, and you don’t want to write your own, check out https://github.com/superwall/iOS-Backports

GitHub

GitHub - superwall/iOS-Backports: SwiftUI-Backports makes it easy to use the latest SwiftUI modifier, like those introduced in iOS 26, while still supporting older iOS versions. With a simple .backport modifier, you can write modern SwiftUI code without wrapping every new API in #available checks. It’s lightweight, drop-in ready, and keeps your UI code clean and future-proof.

SwiftUI-Backports makes it easy to use the latest SwiftUI modifier, like those introduced in iOS 26, while still supporting older iOS versions. With a simple .backport modifier, you can write moder...
  • Copy link
  • Flag this comment
  • Block
Matt Massicotte
@mattiem@mastodon.social replied  ·  activity timestamp last month

@siracusa I have heard very good things about this one too, just in case: https://github.com/shaps80/SwiftUIBackports

(Unsure if it uses the same techniques)

GitHub

GitHub - shaps80/SwiftUIBackports: A collection of SwiftUI backports for iOS, macOS, tvOS and watchOS

A collection of SwiftUI backports for iOS, macOS, tvOS and watchOS - shaps80/SwiftUIBackports
  • Copy link
  • Flag this comment
  • Block
shaps
@shaps@mastodon.social replied  ·  activity timestamp last month

@mattiem @siracusa Hey I'm the developer of SwiftUIBackports. I do indeed use the same approach, Dave and I discussed that approach when we first wrote about it. I have a lot more back ports since I started earlier, but this new lib has a bunch I haven't gotten around to, I'd love to get them contributed if you'd like to contribute? Great work, either way 👍

  • Copy link
  • Flag this comment
  • Block
Gernot
@gernot@mas.to replied  ·  activity timestamp last month

@siracusa @davedelong If the „if" condition does not change at runtime, there are no downsides to a conditional modifier. That's why the „Backport“ System has no real downsides.

  • Copy link
  • Flag this comment
  • Block
Casey Liss
@caseyliss@mastodon.social replied  ·  activity timestamp last month

@siracusa While I'm creeped out you're watching me struggle with this, I appreciate the very timely help.

  • Copy link
  • Flag this comment
  • Block
John Siracusa
@siracusa@mastodon.social replied  ·  activity timestamp last month

@caseyliss I wasn't! This was something I was doing in one of my apps the other day, and I complained about it in a Slack, which led to Dave's suggestion that helped me clean things up considerably.

  • Copy link
  • Flag this comment
  • Block
Log in

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.0-rc.3.21 no JS en
Automatic federation enabled
  • Explore
  • About
  • Members
  • Code of Conduct
Home
Login