-2

This is super frustrating because i've looked at code examples from very recent answers, tutorials, etc. on how to create a send email function (using the built in ios mail) and it's throwing me errors. Specifically, unresolved use of "self identifier" and "present"

func sendEmail(sender: AnyObject) {
    let mailVC = MFMailComposeViewController()
    mailVC.mailComposeDelegate = self
    mailVC.setToRecipients([])
    mailVC.setSubject("Subject for email")
    mailVC.setMessageBody("Email message string", isHTML: false)

    presentViewController(mailVC, animated: true, completion: nil)
}

what example am i doing wrong? Logically, it makes sense; i've created an instance of mailcompose and then assigned its delegate to itself.

looked at these two for use: https://www.hackingwithswift.com/example-code/uikit/how-to-send-an-email Sending an email from swift 3

nathan
  • 9,329
  • 4
  • 37
  • 51
  • 1
    What version of Xcode and Swift are you using? And please include complete and exact error messages in your question and point out exactly which line is causing the error. – rmaddy Jul 17 '17 at 22:42
  • 1
    I did not criticize. I simply asked you to provide further details to make it easier for people to help you. And you should not make assumptions about whether I down voted or not. – rmaddy Jul 18 '17 at 05:39

2 Answers2

2

Make sure you add import MessageUI, and you’ll also need to conform to the MFMailComposeViewControllerDelegate protocol.

Nemanja
  • 194
  • 10
0

Both errors you reported hint that your sendEmail function might have been declared outside a view controller class (i.e., it's a global function). If this is the case, move it to the appropriate UIViewController subclass instead ;)

As correctly pointed out by Nemanja's answer, be sure to import MessageUI framework as well.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85