向邮件正文添加额外的字符串 (swift)

Adding additional string to message body (swift)

我的电子邮件工作正常。我唯一需要做的就是在我的邮件正文中添加一个额外的字符串。例如,我想将 Name: 和文本字段添加到我的邮件正文中。像这样的东西。

姓名:约翰·史密斯

Phone: 566-654-6577

电子邮箱:Smith@smith.com

目前,邮件正文仅显示以下内容。

约翰·史密斯

566-654-6577

史密斯@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()

    }




}

您可以使用 reducezip2 函数来执行类似的操作。

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 "
}

删除 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)
    }

替换 可变字段:[UITextField] = [姓名,phone,电子邮件] var messageBody = ""

    for f in fields {
        messageBody = appendTextFromTextField(messageBody, textField: f)
    }

与: 变种消息体=“” 让 addOn:String = "name: (name.text)\nphone: (phone.text)\nemail:(email.text)\n" messageBody = messageBody + addOn

另外,我不确定你为什么有 tableViewController。如果您不在故事板中放置 table,您将收到运行时错误