Xcode添加多个字符串消息正文

问题描述 投票:2回答:1

我正在尝试发送带有附加字符串的电子邮件。我能够在StackOverflow中找到相关链接:Adding additional string to message body (swift)。我需要帮助,在电话号码后添加一个带有用户输入文字信息的附加字符串。

例: 名称 电子邮件 电话号码 信息

这是我的代码:

@IBOutlet var nameField: UITextField!
@IBOutlet var emailAddressfield: UITextField!
@IBOutlet var phoneNumberfield: UITextField!
@IBOutlet var subjectfield: UITextField!
@IBOutlet weak var requestfield: UITextView!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



@IBAction func Send(sender: Any) {

    var SubjectText = "Prayer Request: "
    SubjectText += subjectfield.text!

    var _: [UITextField] = [nameField, phoneNumberfield, emailAddressfield]

    let toRecipients = ["[email protected]"]

    let MessageBody = requestfield.text!

    let mc: MFMailComposeViewController = MFMailComposeViewController()
    mc.mailComposeDelegate = self
    mc.setSubject(SubjectText)
    mc.setMessageBody(MessageBody, isHTML: false)
    mc.setToRecipients(toRecipients)

    self.present(mc, animated: true, completion: nil)

}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    switch result.rawValue {
    case MFMailComposeResult.cancelled.rawValue:
        print("Mail Cancelled")

    case MFMailComposeResult.saved.rawValue:
        print("sMail Saved")

    case MFMailComposeResult.sent.rawValue:
        print("Mail Sent")

    case MFMailComposeResult.failed.rawValue:
        print("Mail Failed: %@", [error?.localizedDescription])

    default:
        break
    }

    controller.dismiss(animated: true, completion: nil)
}

func dismissKeyboard(_ sender: AnyObject) {

    self.resignFirstResponder()
}
ios swift mfmailcomposeviewcontroller
1个回答
1
投票

您只需在信息正文字符串中附加信息,就像您为subjectText所做的那样。你的send函数应该是这样的:

@IBAction func send(sender: Any) {
    let subjectText = "Prayer Request: " + subjectfield.text!
    let toRecipients = ["[email protected]"]

    var messageBody = requestfield.text!
    messageBody += "\nName: " + nameField.text!
    messageBody += "\nPhone No.: " + phoneNumberfield.text!
    messageBody += "\nEmail: " + emailAddressfield.text!

    let mc: MFMailComposeViewController = MFMailComposeViewController()
    mc.mailComposeDelegate = self
    mc.setSubject(subjectText)
    mc.setMessageBody(messageBody, isHTML: false)
    mc.setToRecipients(toRecipients)

    self.present(mc, animated: true, completion: nil)
}
© www.soinside.com 2019 - 2024. All rights reserved.