`SwiftUI.FileExportOperation.Error error 0` with `fileExporter` and `NSImage`

You'll have to forgive me, I am still pretty new to Swift, but I'm really struggling to figure out what I'm doing wrong or how I can fix it. I'm working on an app that generates an image from some views and then exports that image, but it always returns this very vague error:

The operation couldn’t be completed. (SwiftUI.FileExportOperation.Error error 0.)

Here's most of the program:

import SwiftUI
import UniformTypeIdentifiers

struct ContentView: View {
    @State private var backgroundColor = Color.black
    @State private var fileExporterIsPresented = false
    @State private var image: NSImage?
    @State private var fileExporterErrorAlertIsPresented = false
    @State private var fileExporterErrorDescription: String?

    var body: some View {
        let wallpaper = Rectangle()
            .foregroundStyle(backgroundColor)
            .aspectRatio(16 / 9, contentMode: .fit)
        
        VStack {
            wallpaper
                .clipShape(.rect(cornerRadius: 10))
                .overlay {
                    RoundedRectangle(cornerRadius: 10)
                        .strokeBorder(.separator, lineWidth: 5)
                }
            ColorPicker("Background Color", selection: $backgroundColor, supportsOpacity: false)
            Button("Generate Wallpaper") {
                let renderer = ImageRenderer(content: wallpaper.frame(width: 3840, height: 2160))
                
                image = renderer.nsImage
                fileExporterIsPresented = true
            }
            .fileExporter(
                isPresented: $fileExporterIsPresented,
                item: image,
                contentTypes: [UTType.heic, UTType.png]
            ) { result in
                if case .failure(let error) = result {
                    fileExporterErrorDescription = error.localizedDescription
                    fileExporterErrorAlertIsPresented = true
                }
            }
            .alert("File Exporter Failure", isPresented: $fileExporterErrorAlertIsPresented, actions: {
                Button("OK") {}
            }, message: {
                if let fileExporterErrorDescription {
                    Text(fileExporterErrorDescription)
                }
            })
            .dialogSeverity(.critical)
        }
        .padding()
    }
}

#Preview {
    ContentView()
}
Answered by DTS Engineer in 812904022

@valentinegb Do you have the appropriate App Sandbox capability enabled ?

If you're using use file import modifiers like fileImporter(isPresented:allowedContentTypes:onCompletion:), and file exporter modifiers like fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:) you might need to grant Read Only or Read/Write permission from the User Selected File menu.

Review Accessing files from the macOS App Sandbox article, it covers how to ensure your app has full read and write access to its sandbox container,

@valentinegb Do you have the appropriate App Sandbox capability enabled ?

If you're using use file import modifiers like fileImporter(isPresented:allowedContentTypes:onCompletion:), and file exporter modifiers like fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:) you might need to grant Read Only or Read/Write permission from the User Selected File menu.

Review Accessing files from the macOS App Sandbox article, it covers how to ensure your app has full read and write access to its sandbox container,

`SwiftUI.FileExportOperation.Error error 0` with `fileExporter` and `NSImage`
 
 
Q