Open eMail from application with predefined text in iOS

Hello, I want to open the eMail program from my application, and the body should already be defined. I can open an email, but I don’t know how to define the email body as a given parameter for displaying standard text. Can anybody help? Here is the code that 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") } 
+5
source share
6 answers

You can do this using the MFMailComposeViewController :

 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) 

In addition, you need to implement mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate , where you must reject MFMailComposeViewController

+12
source

If you want to open the embedded email application, rather than displaying MFMailComposeViewController , as others have mentioned, you can build the mailto: link as follows:

 let subject = "My subject" let body = "The awesome body of my email." let encodedParams = "subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) let url = "mailto: foo@bar.com ?\(encodedParams)" if let emailURL = NSURL(url) { if UIApplication.sharedApplication().canOpenURL(emailURL) { UIApplication.sharedApplication().openURL(emailURL) } } 

Just to save someone typing, in 2016 the syntax changed slightly:

 let subject = "Some subject" let body = "Plenty of email body." let coded = "mailto: blah@blah.com ?subject=\(subject)&body=\(body)".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) if let emailURL:NSURL = NSURL(string: coded!) { if UIApplication.sharedApplication().canOpenURL(emailURL) { UIApplication.sharedApplication().openURL(emailURL) } 
+11
source

version of Swift 3

 let subject = "Some subject" let body = "Plenty of email body." let coded = "mailto: blah@blah.com ?subject=\(subject)&body=\(body)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) if let emailURL: NSURL = NSURL(string: coded!) { if UIApplication.shared.canOpenURL(emailURL as URL) { UIApplication.shared.openURL(emailURL as URL) } } 
+11
source

Use the MFMailComposeViewController as follows:

  • Import MessageUI

     import MessageUI 
  • Add a delegate to your class:

     class myClass: UIViewController, MFMailComposeViewControllerDelegate {} 
  • Configure the email pre-set you want to have

     let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setSubject("Subject") mail.setMessageBody("Body", isHTML: true) mail.setToRecipients([" my@email.com "]) presentViewController(mail, animated: true, completion: nil) 
  • Put this method in your code:

     func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) { dismissViewControllerAnimated(true, completion: nil) } 

There you go, working now.

+4
source

I suggest using the Apple method, which you can find in the official documentation on MFMailComposeViewController . It opens the modal view manager with email, which is rejected after sending. Thus, the user remains in your application.

0
source

Swift 4.0

  let email = " feedback@company.com " let subject = "subject" let bodyText = "Please provide information that will help us to serve you better" if MFMailComposeViewController.canSendMail() { let mailComposerVC = MFMailComposeViewController() mailComposerVC.mailComposeDelegate = self mailComposerVC.setToRecipients([email]) mailComposerVC.setSubject(subject) mailComposerVC.setMessageBody(bodyText, isHTML: true) self.present(mailComposerVC, animated: true, completion: nil) } else { let coded = "mailto:\(email)?subject=\(subject)&body=\(bodyText)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) if let emailURL = URL(string: coded!) { if UIApplication.shared.canOpenURL(emailURL) { UIApplication.shared.open(emailURL, options: [:], completionHandler: { (result) in if !result { // show some Toast or error alert //("Your device is not currently configured to send mail.") } }) } } } 
0
source

Source: https://habr.com/ru/post/1235893/


All Articles