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
}
}
}