Documents in SwiftUI app have wrong file extension on Mac

I have a multiplatform document-based SwiftUI app. I have a custom document type with its own file extension that saves documents as file packages that appears as a single file in the Finder or Files app. The app exports documents as EPUB books.

The document type, exported type identifier, and imported type identifier settings are the same for the iOS and Mac app targets.  I have the following code in my document struct for dealing with the document types:

extension UTType {
    static var bookDocument: UTType {
        UTType(importedAs: "com.MyConpany.book")
    }
}

static var readableContentTypes: [UTType] {   
   [.bookDocument] 
}

static var writableContentTypes: [UTType] { 
    [.epub]
}

Everything works right on iOS. The documents appear as single files in the Files app and have my custom file extension. But on Mac the documents have the extension .epub. When I try to open a document from the Finder, the Books app launches instead of opening the document in my app. The document fails to open in the Books app because it’s not an EPUB book.

Fix Attempt 1

I figured the document has the .epub extension because it’s the only writable content type for the document. I tried adding my document type to the list of writable content types.

static var writableContentTypes: [UTType] { 
    [.bookDocument, .epub]}

But the document’s file extension changes to .iba, which is iBooks Author format. Opening the document in the Finder launches Pages instead of my app. From past experience the iBooks Author format is the default document format when no writable content types are supplied.

Fix Attempt 2

My next attempt was to create a UTType with my custom file extension, conform to package, and add that to the writable content types array.

static var bookDocumentPackage: UTType {
    UTType(filenameExtension: "swiftbook", 
        conformingTo: .package)!
}

static var writableContentTypes: [UTType] { 
    [.bookDocumentPackage, .epub]
}

But the document appears as a folder in the Finder instead of a single file.

Fix Attempt 3

My next attempt was to create a UTType that exports to my custom document type and conforms to package.

static var bookDocumentPackage: UTType {
    UTType(exportedAs: "com.MyCompany.book",
        conformingTo: .package)
}

But the document has the extension .book. Doing a Get Info on the document in the Finder shows it’s in iBooks Author format.

Question

What do I have to do with the UTTypes to get my document to appear as a single file in the Finder with my custom file extension?

Accepted Reply

The fix is to make the last part of the custom document UTI match the file extension, com.MyCompany.swiftbook. With that change the following code saves with the correct extension on Mac:

static var bookDocumentPackage: UTType {
    UTType(exportedAs: "com.MyCompany.swiftbook",
        conformingTo: .package)
}

static var writableContentTypes: [UTType] { 
    [.bookDocumentPackage, .epub]
}

Replies

The fix is to make the last part of the custom document UTI match the file extension, com.MyCompany.swiftbook. With that change the following code saves with the correct extension on Mac:

static var bookDocumentPackage: UTType {
    UTType(exportedAs: "com.MyCompany.swiftbook",
        conformingTo: .package)
}

static var writableContentTypes: [UTType] { 
    [.bookDocumentPackage, .epub]
}