When you create an app that deals with files you can ofter deal with standard file types such as .txt or .jpg. When the file format is bespoke to your application, it generally makes sense to define your own file type and an extension to go with it. Defining your own type is fairly straight forward, when you know the bits you need to change.

I wrote some noted about using file dialogs to open and save-as files. In that example I worked with text files because that's a type known to the Mac system, so made the code easier. I will be extending that example in this note to include a custom file type with its own custom file extension.

Our File Extension

For the purposes of this note, we are going to add a file type for files with the extension “xyz”. We’re then going to extend our file selection code to request files with an extension of “xyz”.

Our starting point is the project target and the info.plist to which we need to add a couple of definitions. I would not recommend editing the info.plist directly; there is a perfectly good and helpful designer built into Xcode that we’re going to use. So, select the project file, select the target and click on the Info tab.

File Type Settings

In the list of sections, there is a section for Document Types. Expand this and click the + to add a new document type for our file extension.

File Type Definition

You need to provide

  • The descriptive name of the data file (App Data File).
  • An identifier (this is important) which takes a similar form as your bundle name.

The identifier is important and needs to be unique. A reasonable identifier name will be the reverse URL that you would typically use for your bundle, plus the file extension you intend to use.

This gets you half way there. Next, expand the Exported Type Identifiers and create a new exported type:

File Type Definition

You need to provide:

  • The type description, which you should generally make the same as the document type descriptive name.
  • The identifier, which must be the same as the document type identifier.
  • Conforms To should be set to public.data.
  • Define the Extensions that you want to support for your document type. In this case, we are using “xyz” in both lower and upper case.

Feel free to lookup the rest of the options. For our purposes, this is sufficient. We’re almost ready to go!

Extending UTType

File dialogs are customised to specific file types using Uniform Type Identifiers. There are a number of in-built types and we need to extend this list to include our new file type.

The simplest way to achieve this is to extend UTType to include a definition for our new type.

At the top of the FileHelpers file, add an extension that defines our type.

extension UTType {
    static var dataFileType: UTType {
        UTType("uk.co.sabarnett.xyz")!
    }
}

The name of the variable we define is the name that we are going to refer to in our code for this document type. The name that we use to initialise the UTType is the identifier we defined earlier.

Select Files of Type

Now we have defined our application’s file type and have extended UTType to define the name of the identifier, we can add a function to the FileHelpers that we can call to select a file of this type.

Button("Open xyz file") {
    if let selectedFile = FileHelpers.selectSingleXYZFile(
        withTitle: "Select an ZZZ file") {

        selectedInputFile = selectedFile.path()
    } else {
        selectedInputFile = "No file selected"
    }
}

When we run the app, a new button appears to open files of type .xyz.

Open XYZ Button

When you click it, the open dialog opens and the only selectable files will be those with an .xyz file extension.

Open XYZ Dialog

WARNING

This may not work! If, when you open the file open dialog files of your selected type are not highlighted, it is likely that another app has already registered the file extension. At run time, it is acceptable to have many apps associated with a single file extension and the user gets to decide which app is the default.

See also

File dialogs

Get The Code
Testimonials
Testimonials

Am I really any good?

Don't take my word for my abilities, take a look at other peoples opinions about me.