Skip to content

Instantly share code, notes, and snippets.

@theoknock
Created July 5, 2024 18:50
Show Gist options
  • Save theoknock/dbf30d77741f6b36bfd9a12c504356b0 to your computer and use it in GitHub Desktop.
Save theoknock/dbf30d77741f6b36bfd9a12c504356b0 to your computer and use it in GitHub Desktop.
A basic FileDocument template created by ChatGPT
import SwiftUI
import UniformTypeIdentifiers
struct TextFile: FileDocument {
static var readableContentTypes: [UTType] { [.plainText] }
var text = ""
init(initialText: String = "") {
text = initialText
}
init(configuration: ReadConfiguration) throws {
if let data = configuration.file.regularFileContents {
text = String(decoding: data, as: UTF8.self)
}
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = Data(text.utf8)
return FileWrapper(regularFileWithContents: data)
}
}
struct ContentView: View {
@Binding var document: TextFile
var body: some View {
TextEditor(text: $document.text)
.padding()
.onAppear {
print("ContentView appeared with text: \(document.text)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(document: .constant(TextFile()))
}
}
//
// FileDocumentChatGPTTemplateApp.swift
// FileDocumentChatGPTTemplate
//
// Created by Xcode Developer on 7/5/24.
//
import SwiftUI
@main
struct FileDocumentChatGPTTemplateApp: App {
var body: some Scene {
DocumentGroup(newDocument: TextFile()) { file in
ContentView(document: file.$document)
.onAppear {
print("DocumentGroup content view appeared")
}
}
}
}
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version = "1.0">
<dict>
<key > LSSupportsOpeningDocumentsInPlace </ key>
<true/>
<key > UISupportsDocumentBrowser </ key>
<true/>
<key > CFBundleDocumentTypes </ key>
<array>
<dict>
<key > CFBundleTypeName </ key>
<string > Plain Text Document </ string>
<key > LSHandlerRank </ key>
<string > Owner </ string>
<key > LSItemContentTypes </ key>
<array>
<string>public .plain - text </ string>
</array>
<key > NSDocumentClass </ key>
<string > $(PRODUCT_MODULE_NAME).TextFile </ string>
</dict>
</array>
<key > UTExportedTypeDeclarations </ key>
<array>
<dict>
<key > UTTypeConformsTo </ key>
<array>
<string>public .plain - text </ string>
</array>
<key > UTTypeDescription </ key>
<string > Plain Text Document </ string>
<key > UTTypeIdentifier </ key>
<string > $(PRODUCT_BUNDLE_IDENTIFIER).plaintext </ string>
<key > UTTypeTagSpecification </ key>
<dict>
<key>public .filename- extension </key>
<array>
<string > txt </ string>
</array>
<key>public .mime - type </ key>
<array>
<string > text / plain </ string>
</array>
</dict>
</dict>
</array>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment