Skip to content

Instantly share code, notes, and snippets.

@mhdhejazi
Last active January 10, 2022 18:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhdhejazi/45fdc219f075147ee83bbdb38c48d8cf to your computer and use it in GitHub Desktop.
Save mhdhejazi/45fdc219f075147ee83bbdb38c48d8cf to your computer and use it in GitHub Desktop.
Use NSOpenPanel from a Mac Catalyst app with the help of Dynamic (https://github.com/mhdhejazi/Dynamic)
// macOS App
let panel = NSOpenPanel()
panel.beginSheetModal(for: view.window!, completionHandler: { response in
if let url: URL = panel.urls.first {
print("url: ", url)
}
})
// Mac Catalyst (with Dynamic)
let panel = Dynamic.NSOpenPanel()
panel.beginSheetModalForWindow(self.view.window!.nsWindow, completionHandler: { response in
if let url: URL = panel.URLs.firstObject {
print("url: ", url)
}
} as ResponseBlock)
typealias ResponseBlock = @convention(block) (_ response: Int) -> Void
@mhdhejazi
Copy link
Author

mhdhejazi commented Apr 27, 2020

UIWindow.nsWindow is defined as follows (source):

extension UIWindow {
    var nsWindow: NSObject? {
        Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(self)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment