How do I programmatically dismiss a FileDocument's ContentView?

Is there a way to close or cancel out of a document programmatically?

Replies

Here's a concise example of my question...
Code Block swift
import SwiftUI
@main
struct DocumentApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: DocumentDocument()) { file in
            ContentView(document: file.$document)
        }
    }
}
struct ContentView: View {
    @Binding var document: DocumentDocument
    var body: some View {
        VStack {
            Button(action: {
                // TODO: Dismiss ContentView and show DocumentGroup browser
            }) {
                Text("Close")
            }
            TextEditor(text: $document.text)
        }
    }
}


Would like to know this as well.
Also of note here is that the following code doesn't dismiss the view and the isPresented property is false in the ContentView presented by the DocumentGroup.

Code Block swift
@Environment(\.presentationMode) var presentationMode
struct ContentView: View {
    var body: some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Dismiss")
}
}
}


I am also looking for how to do this. If anyone finds a mechanism, please post it!
I'm having the same issue. Have you found a solution since?
This seems to work, but I'm not sure how brittle it is:

Code Block swift
for window in UIApplication.shared.windows where window.isKeyWindow {
window.rootViewController?.dismiss(animated: true, completion: nil)
break
}