In a normal UIViewController
in Swift, I use this code to send a mail.
let mailComposeViewController = configuredMailComposeViewController()
mailComposeViewController.navigationItem.leftBarButtonItem?.style = .plain
mailComposeViewController.navigationItem.rightBarButtonItem?.style = .plain
mailComposeViewController.navigationBar.tintColor = UIColor.white
if MFMailComposeViewController.canSendMail() {
self.present(mailComposeViewController, animated: true, completion: nil)
} else {
self.showSendMailErrorAlert()
}
How can I achieve the same in SwiftUI?
Do I need to use UIViewControllerRepresentable
?
Answers are correct Hobbes the Tige & Matteo
From the comments, if you need to show an alert if no email is set up on the button or tap gesture
To pre-populate To, Body ...
@Matteo's answer is good but it needs to use the presentation environment variable. I have updated it here and it addresses all of the concerns in the comments.
Usage:
As you mentioned, you need to port the component to
SwiftUI
viaUIViewControllerRepresentable
.Here's a simple implementation:
Usage:
Notes:
I'm using a
ZStack
to show it, as theModal
behaviour was quite inconsistent.(Tested on iPhone 7 Plus running iOS 13 - works like a charm)
Updated for Xcode 11 beta 5