In a SwiftUI app on MacOS I want to allow a users to select a file from the MacOS filesystem.
I try to use AppKits NSOpenPanel .
I tried like this, but I'm not able to create the NSViewControllerRepresentable.
struct ContentView: View {
@State var filename = "Filename"
@State var showFileChooser = false
var body: some View {
HStack {
Text(filename)
Button("select File")
{ self.showFileChooser = true
}.sheet(isPresented: $showFileChooser)
{ FileChooser()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct FileChooser : {
func makeNSViewController(context: Context) -> NSOpenPanel {
NSOpenPanel()
}
func updateNSViewControler(_ nsView: NSOpenPanel, context: Context) {
}
}
Is this the correct approach?
What's wrong?