0

I have a contact us page in my app which shows my Facebook, twitter and website pages. I have managed to get them to work properly by linking a IBAction to the button. The code i used below, however i would like to do the same with my email. When the button is pressed i want to open up the email on the phone and the "To" "subject" fields to be already filled in. I don't want to add a email form to my app as that is the only thing I can find info on. Is there a way i can achieve this?

      import UIKit
       import MessageUI

        class SocialViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



     @IBAction func openWeb(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.xxxxxxxxxxxx.com")!)
   }



   @IBAction func openFB(sender: AnyObject) {

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb://profile/XXXXXXXX")!) {
  UIApplication.sharedApplication().openURL(NSURL(string: "fb://profile/XXXXXXXXXX")!)
} else {
  UIApplication.sharedApplication().openURL(NSURL(string: "https://facebook.com/XXXXXXXXX")!)
}
  }



  @IBAction func openTW(sender: AnyObject) {

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "tw://profile/XXXXXXXXX")!) {
  UIApplication.sharedApplication().openURL(NSURL(string: "fb://profile/XXXXXXX")!)
} else {
  UIApplication.sharedApplication().openURL(NSURL(string: "https://twitter.com/XXXXXXX")!)
}
  }



    }
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
uz7
  • 159
  • 2
  • 15

1 Answers1

1

To send a mail with Swift through the native mail application add MFMailComposeViewControllerDelegate to your class inheritance

And add the following in your action:

var mail: MFMailComposeViewController!

let toRecipients = ["test@mail.com"]
let subject = "Subject"
let body = "Your body text"

mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(toRecipients)
mail.setSubject(subject)
mail.setMessageBody(body, isHTML: true)

presentViewController(mail, animated: true, completion: nil)
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • i get 7 errors. for the line mail = MFMailComposeViewController() value of type 'UIDevice' has no member 'model name' the following 5 lines beginning with mail give the error..... Use of unresolved identifier 'mail' – uz7 Dec 14 '15 at 22:03
  • Check the update, something went wrong when I typed. @uz7 – Rashwan L Dec 14 '15 at 22:03
  • Just one error now. on the line mail.mailComposeDelegate = self Cannot assign value of type 'SocialViewController' to type 'MFMailComposeViewControllerDelegate?' – uz7 Dec 14 '15 at 22:08
  • You have not added MFMailComposeViewControllerDelegate yet. Your class declaration should look like this: class SocialViewController: UIViewController, MFMailComposeViewControllerDelegate – Rashwan L Dec 14 '15 at 22:09
  • That fixed the errors but now the simulator keep crashing when i press the button. Is that because its simulator? and it will work on iOS device? – uz7 Dec 14 '15 at 22:23
  • What error message do you get? Have you connected your outlet in a correct way to the button? – Rashwan L Dec 14 '15 at 22:24
  • working fine now, the mail screen comes up but the "To" and "Body' fields are empty plus the cancel button doesn't take me back to the app for some reason – uz7 Dec 14 '15 at 23:07
  • Have you tried it on your phone? It seems to be a bug in Xcode. – Rashwan L Dec 14 '15 at 23:09
  • no since the new iPhone update my phone is not working in Xcode keeps saying cannot find developer disk image – uz7 Dec 14 '15 at 23:12
  • Ok, but the code above works perfect. To solve your developer disk image issue check http://stackoverflow.com/questions/30736932/could-not-find-developer-disk-image-xcode-7-ios-8-4 Good luck @uz7 – Rashwan L Dec 14 '15 at 23:15