swiftuttype

How do I create a custom UTType with multiple extensions using Swift?


I want to create a custom UTType to support multiple extensions(cxf and rwxf)

It similar to UTType.tiff

UTType.tiff.tags
[
    public.filename-extension: ["tiff", "tif"], 
    public.mime-type: ["image/tiff"], 
    com.apple.nspboard-type: ["NeXT TIFF v4.0 pasteboard type", "NSTIFFPboardType"], 
    com.apple.ostype: ["TIFF"]
]

Currently! I can create with 1 file extension for custom UTType.
How do I create a custom UTType with multiple extensions(cxf and rwxf)?

UTType(tag: "cxf", tagClass: .filenameExtension, conformingTo: .data)

Solution

  • You can add as many file extensions as you want in the "Exported/Imported Type Identifiers" section of your project settings.

    Here is an example where I have added "abc" and "def" as an exported type identifier:

    enter image description here

    Corresponding Info.plist code:

    <key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
            </array>
            <key>UTTypeDescription</key>
            <string>Some Description</string>
            <key>UTTypeIconFiles</key>
            <array>
                <string>icon</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>com.example.example</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>abc</string>
                    <string>def</string>
                </array>
                <key>public.mime-type</key>
                <array/>
            </dict>
        </dict>
    </array>
    

    You can then get this as a UTType in your code like this:

    print(UTType(exportedAs: "com.example.example").tags)
    // prints '[public.filename-extension: ["abc", "def"]]'