I have an app that allows the user to input their contact information and it sends the mail to my email address with the information they put into the textfields. Everything seems to work and I can send the mail with the desired text, however I can't seem to input the information from the textfield. Please help! Im kind of new to this :)
var name: String?
@IBOutlet weak var nameField: UITextField!
var contact: String?
@IBOutlet weak var contactField: UITextField!
var other: String = "nothing"
@IBOutlet weak var otherField: UITextField!
@IBAction func sendEmail(_ sender: Any) {
let name = nameField.text
let contact = contactField.text
if other == nil {
}else{
let other = otherField.text!
}
When I click the button, it pulls up the mail app with information in the body. Here is the code:
func configureMailController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["[email protected]"])
mailComposerVC.setSubject("Contact information")
mailComposerVC.setMessageBody("Another Friend!
My name or business is: (name)
My contact information is: (contact)
My additional information includes: (other)", isHTML: false)
return mailComposerVC
}
Here is the output in the body of the mail app (even when I write things in the textfield):
"Another Friend!
My name or business is: nil
My contact information is: nil
My additional information includes: nothing"
EDIT:
Ive change my sendMail
function to simply contain the outlet names:
func configureMailController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["[email protected]"])
mailComposerVC.setSubject("Staples Day")
mailComposerVC.setMessageBody("Another Friend!
My name or business is: (nameField.text)
My contact information is: (contactField.text)
My additional information includes: (otherField.text)", isHTML: false)
return mailComposerVC
}
And now my output has textfield info:
"Another Friend!
My name or business is: Optional("whatever I put in the textfield")
My contact information is: Optional("whatever I put in the textfield")
My additional information includes: Optional("whatever I put in the textfield")"
But how do I get rid of the: Optional("")
surrounding my string?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…