SwiftUI MacOS File importer/alert issues

Hey, I am making a YouTube downloader for macOS with SwiftUI and I need to use a file importer to allow the user to pick a location to download the video to. My app is a menu bar app and any kind of popover, like alert, sheet, file importer etc. cannot work properly with a menu bar app.

However that’s beside the point because I created another simpler UI to illustrate my point. Here is a screenshot of it (in Google drive because I can only post 4MB in size here.)

Here is the relevant parts of my code:

App entry:

import SwiftUI

@main
struct AppApp: App {
    var body: some Scene {
        MenuBarExtra {
            ContentView()
                .frame(width: 300, height: 300)
        } label: {
            Image(systemName: "heart.fill")
        }
        .menuBarExtraStyle(.window)

    }
}

ContentView:


struct ContentView: View {
    @State private var fileImporterShows = false
    var body: some View {
        VStack {
            Button("Open file importer") {
                fileImporterShows = true
            }
        }
        .padding()
        .fileImporter(isPresented: $fileImporterShows, allowedContentTypes: [.directory]) { _ in
            
        }
    }
}

Hi Daniel,

The screenshot is not visible without the user trying to view it having to provide an email address to request access. Change the permissions on the image in Google Drive to that anyone with the link can view it.

Hello Chris, I’m sorry about that, I’ve updated the permissions so hopefully it will work now.

The canvas places a heart icon on the Mac which disappears when you close the canvas via Xcode. That heart icon does not make the fileImporter respond.

If you build and run the code another heart icon appears and that brings up the fileImporter when you tap on the button

If you change the App code file this:

@main
struct DanielC2App: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
//        MenuBarExtra {
//            ContentView()
//                .frame(width: 300, height: 300)
//        } label: {
//            Image(systemName: "heart.fill")
//        }
//        .menuBarExtraStyle(.window)
    }
}

The fileImporter works fine. You can close it and open it and navigate through whichever folder you like.

Right, thanks Chris! So it seems like I cannot use fileImporter in a menu bar app.

Not necessarily, the App configuration might be the issue and to be honest I don’t know what to suggest since I’m a bit out of my depth.