我的电子邮件工作正常。我唯一需要做的就是在我的邮件正文中添加一个额外的字符串。例如,我想将 Name: 和文本字段添加到我的邮件正文中。像这样的东西。
姓名:约翰·史密斯
电话:566-654-6577
邮箱:Smith@smith.com
目前,消息正文仅显示以下内容。
约翰·史密斯
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()
}
}
最佳答案
删除 for
循环并尝试用这个方法替换您的方法:
@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)
}
关于email - 向消息正文添加额外的字符串(swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28282442/