How to send email from SwiftUI view ?

I've been trying to send email from a SwiftUI view (see code further below).


However, I'm running into the following issues:

  1. Non-class type 'SettingsView' cannot conform to class protocol 'MFMailComposeViewControllerDelegate'
  2. 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)

}

}

Answered by caram in 371161022

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

And there is a 3rd issue by the way:

3. Use of unresolved identifier 'present'

Accepted Answer

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

I am using the same code to send email from SwiftUI and i can send mail with it, but I get no Swooth sound. I really would like the swoosh sound. What might be the problem?
How to send email from SwiftUI view ?
 
 
Q