Skip to content

2. Getting Started

Gabriel Theodoropoulos edited this page Jul 22, 2021 · 1 revision

The simplest way to use EmailComposer is demonstrated in the following code example:

struct ContentView: View {
    @State private var showEmailComposer = false
    
    var body: some View {
        Button("Send email") {
            showEmailComposer = true
        }
        .emailComposer(isPresented: $showEmailComposer)
    }
}

The emailComposer(isPresented:) view modifier will present the system provided email composer controller modally. There are more arguments that you can optionally provide, however isPresented is the only required argument.

Note that in case the device cannot send emails, then the above will present a default view with a predefined message instead of the email composer. To display a custom content, or to avoid displaying anything, see the Not All Devices Can Send Emails section.

Here is another example that makes full use of the emailComposer view modifier, providing all optional arguments:

Button("Send email") { ... }
.emailComposer(isPresented: $showEmailComposer, emailData: emailData, onDismiss: {
    // Perform actions on dismiss...
}, result: { result in
    // Handle email composing result...
}, deviceCannotSendEmailsView: DeviceCannotSendEmailsView(content: {
    // A custom view to display if the device cannot send emails...
}))

The following sections contain more details about everything shown above.

Clone this wiki locally