0

In the code below I want to use .windowResizability only if #available(macOS 13.0, *) == true or @ available(macOS 13.0, *) cause it doesn't available under macOS 13. I can not find the solution by myself.

//
//  Test_HowAviableApp.swift
//  Test HowAviable
//
//  Created by Sebastien REMY on 03/11/2022.
//

import SwiftUI
import UniformTypeIdentifiers

@main
struct Test_HowAviableApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: MyDocument()) { file in
            MyView(document: file.$document)
        }
        // @available(macOS 13.0, *) // <- DOESN'T WORK!
        //.windowResizability(.contentSize) // Only for macOs 13+
    }
}

struct MyDocument: FileDocument, Codable {
    
    static var readableContentTypes = [UTType(exportedAs:"com.test.test")]
    var test = "test"
    init() {
        
    }
    
    init(configuration: ReadConfiguration) throws {
        if let data = configuration.file.regularFileContents {
            self = try JSONDecoder().decode(MyDocument.self, from: data)
        }
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = try JSONEncoder().encode(self)
        return FileWrapper(regularFileWithContents: data)
    }
}

struct MyView: View {
    @Binding var document: MyDocument
    
    var body: some View {
        Text("Hello")
    }
}
Sébastien REMY
  • 2,399
  • 21
  • 39
  • 3
    Does this answer your question? [Check OS version using Swift on Mac OS X](https://stackoverflow.com/questions/36772557/check-os-version-using-swift-on-mac-os-x) – lazarevzubov Nov 03 '22 at 08:35
  • 1
    Does this answer your question? [SwiftUI: using view modifiers between different iOS versions without #available](https://stackoverflow.com/questions/68892142/swiftui-using-view-modifiers-between-different-ios-versions-without-available) Although this is for iOS it will work for macOS. – Andrew Nov 03 '22 at 12:32

3 Answers3

1

In the case of a single expression only you can remove return:

Try this:

struct MacDemoApp: App {
    var body: some Scene {
        
        if #available(macOS 13, *) {
            return DocumentGroup(newDocument: MyDocument()) { file in
                ContentView(document: file.$document)
            }.windowResizability(.contentSize)
        } else {
            return DocumentGroup(newDocument: MyDocument()) { file in
                ContentView(document: file.$document)
            }
        }
    }
}
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
RTXGamer
  • 3,215
  • 6
  • 20
  • 29
0
  1. .windowResizability is only available on macOS, so you need to conditionally apply this modifier based on the platform.
  2. .windowResizability only supports macOS 13.0+, so in macOS implementation, an API Availability Check is needed.

Based on that, here is what I did to add this conditional capability.

@main
struct YourApp: App {
    var body: some Scene {
#if os(macOS)
        // macOS
        conditionalWindowScene()
#else
        // Other Platform
        WindowGroup {
            ContentView()
        }
#endif
    }

#if os(macOS)
    /// Extract your conditional scene to avoid using `@SceneBuilder`
    /// In `body`, SwiftUI will always use `@SceneBuilder` to build multiple Scene.
    /// Because the result type is `some Scene`,
    /// you just need to return a type that conforms to `Scene` Protocol.
    func conditionalWindowScene() -> some Scene {
        if #available(macOS 13.0, *) {
            /// `Window` Scene is only available on macOS 13.0+
            return Window("App", id: "MAIN") {
                ContentView()
            }
        } else {
            /// Otherwise, using `WindowGroup`
            return WindowGroup {
                ContentView()
            }
        }
    }
#endif
}

Window Scene is only available on macOS 13.0+ , which is very similar to your question.

Hope this can help you.

  • You can also update your Xcode beta because the latest beta adds `buildExpression`, `buildLimitedAvailability` and `buildOptional` to `SceneBuilder`, so you can directly use if-condition to optionally add scenes. – LiYanan2004 Feb 18 '23 at 07:45
-1

Just wrap it with if statement

if #available(macOS 13, *) {
    .windowResizability(.contentSize)
}