SwiftUI conditional modifier depending on OS type and version

This app may run on MacOS or iOS.

I want to use windowResizability modifier (specially In MacOS) which is only available on masOS 13+ and iOS 17+, but still need to run on macOS 12 or iOS 15…

So I need something like

#if os(macOS)
  if #available(macOS 13 *) {
       use windowResizability
    #else
       do not use windowResizability
    #endif

#else //  iOS
  if #available(iOS 17 *) {
       use windowResizability
    #else
       do not use windowResizability
#endif

Here is the code where to apply (in @main)

struct TheApp: App {

    var body: some Scene {
        WindowGroup {
            ContentView()  // 1.11.2023
                .frame(
                    minWidth: 1200, maxWidth: .infinity,
                    minHeight: 600, maxHeight: .infinity)  
        }
       .windowResizability(.contentSize)   // BTW: is that really necessary ?
    }
}

How can I achieve this ? Do I need to write a WindowGroup extension for the modifier ? If so, how ?

BTW: is windowResizability really necessary ? App seems to work the same without it.

Answered by Claude31 in 807941022

I found a simple answer here: https://stackoverflow.com/questions/74300019/how-to-use-if-available-in-main-in-a-documentgroup-application

Just enclose in a func:

struct TheApp: App {
    
    var body: some Scene {
        conditionalScene()
    }

     #if os(macOS)

    func conditionalScene() -> some Scene {
        
        if #available(macOS 13.0, *) {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
            .windowResizability(.contentSize)  // Only here
            
        } else {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
        }
    }
    #else //      iOS

    func conditionalScene() -> some Scene {
        if #available(iOS 17.0, *) {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
            .windowResizability(.contentSize)  // Only here
        } else {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
        }
    }
    #endif    // test of OS

} // App
Accepted Answer

I found a simple answer here: https://stackoverflow.com/questions/74300019/how-to-use-if-available-in-main-in-a-documentgroup-application

Just enclose in a func:

struct TheApp: App {
    
    var body: some Scene {
        conditionalScene()
    }

     #if os(macOS)

    func conditionalScene() -> some Scene {
        
        if #available(macOS 13.0, *) {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
            .windowResizability(.contentSize)  // Only here
            
        } else {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
        }
    }
    #else //      iOS

    func conditionalScene() -> some Scene {
        if #available(iOS 17.0, *) {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
            .windowResizability(.contentSize)  // Only here
        } else {
            WindowGroup {
                ContentView()
                    .frame(
                        minWidth: 1200, maxWidth: .infinity,
                        minHeight: 600, maxHeight: .infinity)
            }
        }
    }
    #endif    // test of OS

} // App
SwiftUI conditional modifier depending on OS type and version
 
 
Q