In a normal UIViewController in Swift, I use this code to send a mail.
let mailComposeViewController = configuredMailComposeViewController()
ma
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
@State var isShowingMailView = false
@State var alertNoMail = false
HStack {
Image(systemName: "envelope.circle").imageScale(.large)
Text("Contact")
}.onTapGesture {
MFMailComposeViewController.canSendMail() ? self.isShowingMailView.toggle() : self.alertNoMail.toggle()
}
// .disabled(!MFMailComposeViewController.canSendMail())
.sheet(isPresented: $isShowingMailView) {
MailView(result: self.$result)
}
.alert(isPresented: self.$alertNoMail) {
Alert(title: Text("NO MAIL SETUP"))
}
To pre-populate To, Body ...
func makeUIViewController(context: UIViewControllerRepresentableContext) -> MFMailComposeViewController {
let vc = MFMailComposeViewController()
vc.setToRecipients(["your@mail.com"])
vc.setMessageBody("You're so awesome!
", isHTML: true)
vc.mailComposeDelegate = context.coordinator
return vc
}