I'm trying to show the file picker window from my macOS GUI app written with SwiftUI. So I call this:
let panel = NSOpenPanel()
//panel.allowedFileTypes = ["myext"] //I get a warning here that it's deprecated
//Trying to use the following instead:
let fileType = UTType(tag: "myext", tagClass: .filenameExtension, conformingTo: nil)
if(fileType != nil)
{
panel.allowedContentTypes = [fileType!]
}
else
{
assert(false)
}
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
panel.canChooseFiles = true
let resp = panel.runModal()
The panel is shown, but I can't select any file, including the ones with my .myext extension.
But if I uncomment panel.allowedFileTypes line, everything works as I expected.
So what am I doing wrong with that allowedContentTypes?
PS. Or, at least how do I mute that deprecated warning in Swift? (The allowedFileTypes works fine, what's the point of deprecating it!)
If it is your own extension, add an “exported type identifier”. If it is a type defined by another app, add an “imported type identifier”.
We can define type identifiers in the “Info” tab of the target settings. That will create/update the
Info.plistaccordingly. For example, if it is an exported identifier:Please make a note of the “identifier”, the associated “extensions”, and the “conforms to” settings.
Having done that, we can now use that identifier in our code. Because this example was an “Exported Type Identifier”, we would use
UTType(exportedAs:):And then:
For more information, see Apple “Tech Talk” video, Uniform Type Identifiers — a reintroduction.