I want to create a SwiftUI macOS app that creates its own data type. It should create files with the extension .frogmd, which is supposed to be a package containing
- a json file representing the document
- a
imagesfolder containing images
Right now, I am able to save my content to disk in a folder containing with above mentioned structure, but it does not create a file with my desired extension, representing a package with the content.
My educated guess is that I am doing something wrong in the Exported Type Identifiers section in my Info.plist, so here's that part of my plist:
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>Frog's MD Document</string>
<key>UTTypeIcons</key>
<dict/>
<key>UTTypeIdentifier</key>
<string>ch.appfros.frogmd</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>frogmd</string>
</array>
</dict>
</dict>
</array>
In case it should help, here's the application's code so far:
@main
struct poc_Markdown2App: App {
var body: some Scene {
DocumentGroup(newDocument: FrogMdDocumentWrapper()) { file in
ContentView(document: file.$document.document)
}
}
}
struct FrogMdDocument: Codable {
var id: UUID
var title: String
var textContent: String
var imageUrls: [URL]
init(id: UUID = .init(), title: String = "New Document", textContent: String = .init(), imageUrls: [URL] = .init()) {
self.id = id
self.title = title
self.textContent = textContent
self.imageUrls = imageUrls
}
}
struct FrogMdDocumentWrapper: FileDocument {
var document: FrogMdDocument
static var readableContentTypes = [UTType.data]
init(document: FrogMdDocument = FrogMdDocument()) {
self.document = document
}
init(configuration: ReadConfiguration) throws {
self.init()
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
do {
let data = try getDocumentData()
let jsonFileWrapper = FileWrapper(regularFileWithContents: data)
let filename = "document.json"
jsonFileWrapper.filename = filename
//TODO: store images to this imagesFileWrapper
let imagesFileWrapper = FileWrapper(directoryWithFileWrappers: [String : FileWrapper]())
let fileWrapper = FileWrapper(directoryWithFileWrappers: [
filename: jsonFileWrapper,
"images": imagesFileWrapper
])
return fileWrapper
} catch {
throw error
}
}
private func getDocumentData() throws -> Data {
let encoder = JSONEncoder()
do {
let data = try encoder.encode(document)
return data
} catch {
throw error
}
}
}
