iosswiftxcodemfmailcomposer

Find string in messageBody of MFMailComposeViewController


I've implemented a standard way for the user to provide feedback. Is there a way to search through the mail's message (or subject) when the user taps on send or cancel in the mail view controller. The app should perform an action if the mail contains a certain string. The mail need not be sent in that case. Thank you

extension AboutViewController: MFMailComposeViewControllerDelegate {

  // MARK: E-Mail

  func configureMailComposerViewController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["support@myapp.app"])

    let prefix = NSLocalizedString("Feedback", comment: "")
    let title = "\(prefix) - myApp v. \(dataModel.appVersion) / iOS \(UIDevice.current.systemVersion) / \(UIDevice.current.deviceModel())"
    mailComposerVC.setSubject(title)
    let localisedGreeting = NSLocalizedString("Hi", comment: "")
    let localisedMessage = NSLocalizedString("I would like to share the following feedback: ", comment: "")
    mailComposerVC.setMessageBody("""
      \(localisedGreeting),

      \(localisedMessage)

      """,
      isHTML: false)

    return mailComposerVC
  }

  func showSendMailErrorAlert() {
    let sendMailErrorAlert = UIAlertController(title: NSLocalizedString("Mail could not be sent", comment: ""),
                                               message: NSLocalizedString("Please check the email configuration in the device settings and try again.", comment: ""),
                                               preferredStyle: .alert)

    sendMailErrorAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))

    present(sendMailErrorAlert, animated: true, completion: nil)
  }

  func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {

// Can I get to the message here? I can't find the property of the controller.

    dismiss(animated: true, completion: nil)
}

Solution

  • There is no public API available that lets you read the contents of the subject or body of the user's email. The MFMailComposeViewControllerDelegate protocol only notifies you if the user did send the email or decided to cancel.

    As per the documentation here, you can customise the fields of the email before presenting to the user but those can be changed.

    Before presenting the interface, populate the fields with initial values for the subject, email recipients, body text, and attachments of the email. After presenting the interface, the user can edit your initial values before sending the email.

    and also says that the user needs to approve the contents of the email and whatever is there is sent to the mail app when the user taps send.

    The composition interface does not guarantee the delivery of your email message; it only lets you construct the initial message and present it for user approval. ... If the user opts to send the message, the message is queued in the user’s Mail app outbox. The Mail app is ultimately responsible for sending the message.