Open a FileDialog in SwiftUI on MacOs
Actually you don't need to, because NSOpenPanel
is a window, not a view controller.
Here is possible approach. Tested with Xcode 11.7 / macOS 10.15.6
struct ContentView: View {
@State var filename = "Filename"
@State var showFileChooser = false
var body: some View {
HStack {
Text(filename)
Button("select File")
{
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
if panel.runModal() == .OK {
self.filename = panel.url?.lastPathComponent ?? "<none>"
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
How do I get the file open dialog to work, when called through a SwiftUI WKWebView inside a macOS native app?
You have to implement the following WKUIDelegate
delegate method
/** @abstract Displays a file upload panel.
@param webView The web view invoking the delegate method.
@param parameters Parameters describing the file upload control.
@param frame Information about the frame whose file upload control initiated this call.
@param completionHandler The completion handler to call after open panel has been dismissed. Pass the selected URLs if the user chose OK, otherwise nil.
If you do not implement this method, the web view will behave as if the user selected the Cancel button.
*/
@available(OSX 10.12, *)
optional func webView(_ webView: WKWebView, runOpenPanelWith
parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping ([URL]?) -> Void)
Here is example of implementation
func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) {
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = true
openPanel.begin { (result) in
if result == NSApplication.ModalResponse.OK {
if let url = openPanel.url {
completionHandler([url])
}
} else if result == NSApplication.ModalResponse.cancel {
completionHandler(nil)
}
}
}
SwiftUI MacOSX ImagePicker
initialize the panel like this:
let myFiledialog = NSOpenPanel()
myFiledialog.prompt = “Select path”
to add a selectbox like you want you need to add this:
myFiledialog.worksWhenModal = true
myFiledialog.canChooseDirectories = false
myFiledialog.canChooseFiles = true
you can select the datatype of your choice with
myFiledialog.allowedFileTypes = [“png”, “jpg”, “jpeg”]
to disable the multiselect add this.
myFiledialog.allowsMultipleSelection = false
i hope this helped you.
macOS SwiftUI: MenuItem to open default browser to a URL?
It should be as follows
@IBAction func Google(_ sender: NSMenuItem) {
if let url = URL(string: "http://google.fi") {
NSWorkspace.shared.open(url)
}
}
SwiftUI 3.0 for macOS: how to set .frame to open at maximum screen space
you could try using:
.frame(minWidth: NSScreen.main?.frame.width, minHeight: NSScreen.main?.frame.height)
works for me on macos 12.1 beta
Related Topics
Is the Swift Divide "/" Operator Not Working or Have I Missed Something
Swift Can't Infer Generic Type When Generic Type Is Being Passed Through a Parameter
Swift Operator "*" Throwing Error on Two Ints
Can't Form Range with End < Start Check Range Before Doing for Loop
Naming Convention for Optional Binding
Using Delegates on Generic Protocol
Multiple Enum Types List All Cases
How to Hide Status Bar and Navigation Bar When Tap Device
How to Make a Swift Enum with Associated Values Equatable
Uitextview Change Text Color of Specific Text
Redeclaring Members in an Extension Hides the Original Member *Sometimes*. Why
Open a Filedialog in Swiftui on MACos
Swift: Programmatically Enumerate Outgoing Segues from a Uiviewcontroller
How to Avoid That My Swift Async Method Runs on the Main Thread in Swiftui
Swiftui Out of Index When Deleting an Array Element in Foreach