I've been trying to send email from a SwiftUI view (see code further below).
However, I'm running into the following issues:
- Non-class type 'SettingsView' cannot conform to class protocol 'MFMailComposeViewControllerDelegate'
- But if make SettingsView a class, then I get a ton of errors.
Has anyone found a good way to solve this issue?
import SwiftUI
import MessageUI
struct SettingsView : View, MFMailComposeViewControllerDelegate {
var body: some View {
Button(action: emailSupport) {
Text("Email support")
}
}
func emailSupport() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setSubject("Some subject")
mail.setToRecipients(["someemail@example.com"])
mail.setMessageBody("Some body", isHTML:false)
present(mail, animated: true)
} else {
// show failure alert
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
}
I have since found this answer to be helpfull. I've gone for the solution in the 2nd answer.
https://stackoverflow.com/questions/56784722/swiftui-send-email