Check if iOS 14 is available in SwiftUI body

Hi,
I am struggling to add new views only available on iOS 14 while maintaining compatibility with iOS 13.

What I'd like is using a LazyVStack on iOS 14 and a List on iOS 13.

The following code compiles correctly but I am getting a ETCBADACCESS error on runtime on iOS 13.

If I switch to VStack instead of LazyVStack (so a view available in iOS 13), the crash disappears.

So it looks like SwiftUI tries to run the code in the #available block even though it's running iOS 13.

Do you have any suggestions ?

Thanks

Code Block swift
var body: some View {
    Group {
        if #available(iOS 14.0, *) {
            ScrollView {
                LazyVStack {
                    content
                        .padding(.horizontal, 15)
                }
            }
        } else {
            List {
                content
            }
        }
    }
}


Post not yet marked as solved Up vote post of NicoD Down vote post of NicoD
27k views

Replies

Bitten by this today; spent all morning trying to debug issue using remote devices in the hands of testers and some AWS Device Farm instances.

If I implement a view that has a conditional check for iOS 15 or newer, my iOS 14 release builds crash when the parent view includes the implemented view.

In order to avoid the crash, I've had to implement two versions of my view - an iOS15+ View and and "older" view. I then use a conditional check in the parent view to determine which of the two subviews to call.

Not great, but at least it stops things from crashing.

I have Xcode Version 13.3 (13E113) and have a similar compiler error Expected declaration on line:

 if #available(iOS 15, *) {

Inside a View struct. FYI.