问题
I got my email working fine. The only thing i need to do is add an additional string to my message body. For example, I want to add Name: and textfield to my message body. Something like this.
Name: John Smith
Phone: 566-654-6577
Email: Smith@smith.com
Right now, the message body only show the following.
John Smith
566-654-6577
Smith@smith.com
import UIKit
import MessageUI
class EmailTableViewController: UITableViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var name: UITextField!
@IBOutlet weak var phone: UITextField!
@IBOutlet weak var email: UITextField!
func appendTextFromTextField(string: String, textField: UITextField) -> String {
return string + textField.text + "\n \n"
}
@IBAction func SendEmailButton(sender: AnyObject) {
var fields: [UITextField] = [name, phone, email]
var messageBody = ""
for f in fields {
messageBody = appendTextFromTextField(messageBody, textField: f)
}
var emailTitle = "Interface Information"
var toRecipents = [""]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: %@", [error.localizedDescription])
default:
break
}
self.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
回答1:
Remove the for
loop and try replacing your method with this one:
@IBAction func sendEmailButton(sender: UIButton) {
var messageBody = "Name:\(name.text)\nPhone:\(phone.text)\nEmail:\(email.text) "
var emailTitle = "Interface Information"
var toRecipents = [""]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
回答2:
You can use the reduce
and zip2
functions to do something like this.
let fields: [UITextField] = [name , phone, email]
let fieldNames : [String] = ["Name", "Phone" ,"Email"]
let messageBody = reduce(Zip2(fields,fieldNames),"") { body,zipped in
let (field,fieldName) = zipped
return body + fieldName + " : " + field.text + " \n\n "
}
回答3:
Replace the var fields: [UITextField] = [name, phone, email] var messageBody = ""
for f in fields {
messageBody = appendTextFromTextField(messageBody, textField: f)
}
with: var messageBody = "" let addOn:String = "name: (name.text)\nphone: (phone.text)\nemail:(email.text)\n" messageBody = messageBody + addOn
Also, I am not sure why you have a tableViewController. If you dont put a table in your storyboard you will get a runtime error
来源:https://stackoverflow.com/questions/28282442/adding-additional-string-to-message-body-swift