-4

I am VERY new to Swift/xcode and I've built my first app but I'm stuck on one part. I have a button that opens an email and I would like to take the selections from a picker, two date pickers and a text input and import them into the email. I want the user to make their choices and just click send when the email opens. Everything works fine but I can't figure out how to import the data from the app into the email despite searching all over. Any suggestions?

}

func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self 

mailComposerVC.setToRecipients(["myemail@myemail.com"])
mailComposerVC.setSubject("EQUIPMENT RESERVATION REQUEST")
mailComposerVC.setMessageBody("test email message", isHTML: false)

return mailComposerVC
}

Basically, I want to replace the "test email message" string with the selections from my date pickers, picker and text input.

gwpeaks
  • 13
  • 4
  • You can use `MFMailComposeViewController`, see this answer: https://stackoverflow.com/a/25984329/1316346 – Kevin DiTraglia Jun 20 '17 at 20:06
  • It's unclear what a useful answer would look like here. Where are you stuck and what have you tried? Can you collect data from these inputs? Can you construct the email body you want to offer to the user? Can you use a mail compose view controller to present an email template (even if it doesn't contain the data you want yet)? Without any visibility into your implementation its very hard to know where you might be stuck or how it might need to change to accomplish what you want to do. – Jonah Jun 20 '17 at 20:59
  • I'm at the point where my template opens correctly via the "submit" button I created but it currently only contains a default message. This is outside of the app via iOS. I'm trying to figure out how to collect the data from my pickers and text box when the button is clicked and have the info in the message body so the user simply needs to send the email. I've tried creating a global variable and embedding a function but neither was successful. That said, I'm very new to this so it is likely I just botched it altogether. Kevin I will try your suggestion as soon as I can. Thanks to you both. – gwpeaks Jun 20 '17 at 21:23
  • I'll post the code when I'm back to my mac. – gwpeaks Jun 20 '17 at 21:24

2 Answers2

0

I was able to figure this out using the link from Kevin above and the following links:

How to store data from a picker view in a variable when a button is pressed?

Get just the date (no time) from UIDatePicker

The revised code is below and works exactly as I wanted it to:

gwpeaks
  • 13
  • 4
0
 @IBOutlet weak var StartDate: UIDatePicker!

 @IBOutlet weak var EndDate: UIDatePicker!

 @IBOutlet weak var HowMany: UITextField!

    func configuredMailComposeViewController() -> MFMailComposeViewController {
        let chosen = Picker1.selectedRow(inComponent: 0)
        let chosenString = words[chosen]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd"
        let stringDate = dateFormatter.string(from: StartDate.date)
        let Start = stringDate
        let number = HowMany.text
        let stringendDate = dateFormatter.string(from: EndDate.date)
        let End = stringendDate
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self
        mailComposerVC.setToRecipients(["myemail@example.com"])
        mailComposerVC.setSubject("Email Subject")
        mailComposerVC.setMessageBody ("Please reserve \(number ?? "") of 
        the following   **\(chosenString)**   from \(Start) to \(End). 
        Thanks.", isHTML: false)
        return mailComposerVC
    }
gwpeaks
  • 13
  • 4