<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "visionOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/NewDocumentButton/init(_:contentType:prepareDocumentURL:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI17NewDocumentButtonVA2A4TextVRszrlE_11contentType07prepareD3URLACyAEG10Foundation23LocalizedStringResourceV_07UniformH11Identifiers6UTTypeVAI0J0VSgyYaKctcfc::OverloadGroup"
  },
  "title" : "init(_:contentType:prepareDocumentURL:)"
}
-->

# init(_:contentType:prepareDocumentURL:)

Creates and opens new documents.

```
@export(implementation) nonisolated init(_ title: LocalizedStringResource, contentType: UTType, prepareDocumentURL: @escaping () async throws -> URL? = { nil })
```

## Parameters

`title`

A title resource for the button title.

`contentType`

A content type of the document to create.

`prepareDocumentURL`

A closure that is called when a user presses the
button. At this point, you can present a document template picker or
another UI that allows users to choose a theme, configuration, or a
template to create a document from.
Return a prepared document, or throw an error if document creation
failed.
Return `nil` to request creation of an empty document.

## Discussion

This initializer allows presenting a template picker,
where a document can be prepopulated or preconfigured using a template.

```
@State private var isTemplatePickerPresented = false
@State private var documentCreationContinuation:
    CheckedContinuation<URL?, any Error>?

var body: some Scene {
    DocumentGroupLaunchScene("My Documents") {
        NewDocumentButton("Start Writing…")
        NewDocumentButton("Choose a Template") {
            try await withCheckedThrowingContinuation { continuation in
                documentCreationContinuation = continuation
                isTemplatePickerPresented = true
            }
        }
        .fullScreenCover(isPresented: $isTemplatePickerPresented) {
            TemplatePicker(
                continuation: $documentCreationContinuation
            )
        }
    }

    DocumentGroup(newDocument: TextDocument()) { configuration in
        MyDocumentView(document: configuration.$document))
    }
}

struct TemplatePicker: View {
    @Binding var continuation:
        CheckedContinuation<URL?, any Error>?
    @Environment(\.dismiss) var dismiss

    var body: some View {
        VStack {
            Text("Choose a template")
                .font(.title)
            Button("Meeting minutes") {
                let document = makeMeetingMinutes()
                documentCreationContinuation?.resume(returning: document)
                dismiss()
            }
            Button("Letter") {
                let document = makeLetter()
                documentCreationContinuation?.resume(returning: document)
                dismiss()
            }
            Button("Cancel") {
                documentCreationContinuation?.resume(throwing: CancellationError())
                dismiss()
            }
        }
    }

    private func makeMeetingMinutes() -> URL { ... }
    private func makeLetter() -> URL { ... }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)