I'm trying to implement sending email feature into my mini app.
Here's the code I'm using (took it from https://hackingwithswift.com):
import Foundation
import SwiftUI
import MessageUI
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["[email protected]"])
mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true)
present(mail, animated: true)
} else {
// show failure alert
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
When running my code, I get these 2 errors:
Cannot find self in scope
Cannot find present in scope
How can I fix it?
You can use
UIViewControllerRepresentableMailComposeViewControllerUsage:
Possible another solution. You can create one singleton class and present
MFMailComposeViewControlleron the root controller. You can modify function as per your requirement. Like thisUsage: