iosswiftemailurl-scheme

Open eMail from App with predefined text in iOS


Hello i want to open the eMail program from my App and the body should already be defined. I can open the eMail but don't know how to define the body of the eMail as a given Parameter to show a given standard text. Anyone can help? Heres the code i use to open Email:

//EMAIL
let email = "foo@bar.com"
let urlEMail = NSURL(string: "mailto:\(email)")

if UIApplication.sharedApplication().canOpenURL(urlEMail!) {
                UIApplication.sharedApplication().openURL(urlEMail!)
} else {
print("Ups")
}

Solution

  • You can do it using MFMailComposeViewController:

    import MessageUI
    
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setToRecipients(["email@email.com"])
    mailComposerVC.setSubject("Subject")
    mailComposerVC.setMessageBody("Body", isHTML: false)
    self.presentViewController(mailComposerVC, animated: true, completion: nil)
    

    Also, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate where you should dismiss MFMailComposeViewController