History
Loading...
Loading...
September 17, 2025
DocumentGroup instead of WindowGroup when building document-based apps. It automatically provides multi-window support, document management, and file handling - just conform your data model to FileDocument and SwiftUI handles the rest with @DocumentGroupEnvironment.Always track document state changes and integrate with the system's undo manager when using DocumentGroup. This creates a professional document editing experience with proper change tracking and undo/redo functionality.
struct ContentView: View {
@Binding var document: MyDocument
@Environment(\.undoManager) var undoManager
@State private var hasUnsavedChanges = false
var body: some View {
TextEditor(text: $document.content)
.onChange(of: document.content) { _, newValue in
hasUnsavedChanges = true
undoManager?.registerUndo(withTarget: document) { doc in
doc.content = document.content
}
}
.toolbar {
ToolbarItem(placement: .status) {
if hasUnsavedChanges {
Label("Unsaved Changes", systemImage: "circle.fill")
.foregroundColor(.orange)
}
}
}
}
}DocumentGroup apps should behave like native document editors. Users expect undo/redo functionality and visual indicators of unsaved changes. This pattern ensures your app integrates seamlessly with macOS document handling conventions and provides the familiar experience users expect from professional applications.