Composing Emails in SwiftUI Using A View Modifier

Gabriel Theodoropoulos
11 min readJun 17, 2021
Code snippet screenshot highlighting the topic of the article, containing the implementation of a Button in SwiftUI with title “Send Email” and of a custom view modifier called email composer that demonstrates the final result of this post.

Just a couple of weeks ago, I had discussed in this previous post about the MFMailComposeViewController; a class that allows to present and use a system provided view controller in order to compose and send emails through our own applications. The discussion on that post was around a UIKit based application explaining how that class works.

In this post today, I’m going to talk about how to integrate MFMailComposeViewController in SwiftUI projects, since there is no native SwiftUI alternative. But that’s going to be just the half of the content; the other half is a step by step guidance how to hide everything behind a custom view modifier, turning that way email composing into a deeply SwiftUI-looking like and straightforward task.

To get a taste, this is what we are going to end up with:

Button("Send Email") {
showEmailComposer = true
}
.emailComposer(isPresented: $showEmailComposer,
emailData: emailData) { result in
// Handle send results.
}

Note that I will not focus on the details of MFMailComposeViewController in this post. I’ve covered that in the first post, so all I’ll do here is to demonstrate how to integrate it in SwiftUI.

The EmailData type

--

--

Gabriel Theodoropoulos
Gabriel Theodoropoulos

Written by Gabriel Theodoropoulos

An iOS & macOS app maker writing code in Swift. Author of countless programming tutorials. Content creator. https://serialcoder.dev

No responses yet