Composing Emails in SwiftUI Using A View Modifier
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.