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

标签 ios swift mfmailcomposeviewcontroller

我正在尝试发送带有附加字符串的电子邮件。我能够在 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 = ["st.johnamechurch@att.net"]

    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()
}

最佳答案

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

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

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

关于ios - Xcode 添加多个字符串消息正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54864763/

相关文章:

iphone - 在 MFMailComposeViewController 中获取收件人列表

ios - MacOS应用程序已针对App Store进行验证:与iOS应用程序兼容的配置文件存在问题

ios - 应用程序的TestFlight分发中的到期日期

ios - 在 Swift 中同时取消两个 ViewController

ios - Swift - 如何更改 accessoryType (disclosureIndicator) 的颜色?

ios - MLMultiArray 使用未声明的类型错误

iOS11 UIToolBar 内容 View

ios - 为 UIView/Image 设置 alpha

ios - 在 ios 中单击 TextView 中的电子邮件 ID

ios - 在 iOS 中关闭地址簿