iosswiftuitableviewmfmailcomposeviewcontroller

How can i insert/pass a value into a mail body from my Table View Cell in Xcode/Swift?


Well I have a problem with my app. Just to say I m a beginner :) I have TableViewControler and I am using func with dequeueReusableCell to return a cell I that is ok. But the problem is how to implement this cell(s) with their values into a body mail using MFMailComposeViewController.setMessageBody? I m trying to pass an order which is in cells just like it is - in a table. That table a need to be visible in mail body. This parameter only takes String and cell is not visible (in scope here). Maybe should I transform cell into a html? And how should I do that?

//This values/cells I need to implement into a mail body//

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "OrderProductsCell", for: indexPath) as! OrderViewCell

    let rowData = orderedProducts[indexPath.row]
    cell.lblNameOfOrderProduct.text = rowData.selectedProducts.name + " " +  rowData.selectedProducts.packaging + " " + rowData.selectedProducts.volume + " " + String(rowData.stepperNumber) + " x(kom)"
    cell.lblFromDistributor.text = "Od"
    cell.lblToStore.text = "Za"
    cell.tfOrderFromDistibutor.text = String(rowData.distibutor)
    cell.tfOrderToStore.text = String(rowData.store)
    cell.tfOrderFromDistibutor.isUserInteractionEnabled = false
    cell.tfOrderToStore.isUserInteractionEnabled = false
   
  
    return cell
    
}

//This is MFMailComposeViewControlles - in the same Controller like Table View

func showMailComposer() {
    guard MFMailComposeViewController.canSendMail() else {
       defAlert(name: "You Can't sent mail", message: "")
        return
    }
    
    let composer = MFMailComposeViewController()
    composer.mailComposeDelegate = self
    composer.setToRecipients(["sample@.com"])
    composer.setSubject("Porudzbina")
    composer.setMessageBody ("Here i need to implement cell", isHTML: false) 
    present(composer,animated: true)
}

Solution

  • I am not sure I understand your end goal completely, but let me share what I think you mean.

    I believe you want all the texts from your table view to show inside the MailView as text.

    And I understand all your products data is inside orderedProducts - correct me if I am wrong ?

    So inside your showMailComposer function, you can maybe try this - I have added comments to new code I have added to your function:

    func showMailComposer() {
        guard MFMailComposeViewController.canSendMail() else {
           defAlert(name: "You Can't sent mail", message: "")
            return
        }
        
        // Start empty string
        var stringBuilder = ""
        
        // Loop through all your products
        for orderedProduct in orderedProducts
        {
            // Append string builder with products
            // Add all product properties you want
            stringBuilder += "\(orderedProduct.name), \(orderedProduct.packaging) \n"
        }
        
        let composer = MFMailComposeViewController()
        composer.mailComposeDelegate = self
        composer.setToRecipients(["sample@.com"])
        composer.setSubject("Porudzbina")
        
        // Add string builder as text
        composer.setMessageBody (stringBuilder, isHTML: false)
        
        present(composer,animated: true)
    }
    

    Run this result and check if it is close to what you want, it will give an output like this:

    MFMailComposeViewController build text to send an email in iOS of all the data in a UITableView

    Update

    If you want to format the text a bit better in the MFMailComposeViewController you can use HTML and you need to know some basics of HTML like table as you suggested or other techniques like div.

    Here is example of your function using table to organize the output:

    func showMailComposer() {
        guard MFMailComposeViewController.canSendMail() else {
           defAlert(name: "You Can't sent mail", message: "")
            return
        }
        
        // Start htmlStringBuilder with opening table tag and 2 headers / columns
        // named Name and Packaging
        var htmlStringBuilder = "<table border = 1><tr><th style=\"padding:10px\">Name</th><th style=\"padding:10px\">Packaging</th></tr>"
        
        // Loop through all your products
        for orderedProduct in orderedProducts
        {
            // Start a new table row
            htmlStringBuilder += "<tr>"
            
            // Add name table data for this row in first column
            htmlStringBuilder += "<td>\(orderedProduct.name)</td>"
            
            // Add packaging table data for this row in second column
            htmlStringBuilder += "<td>\(orderedProduct.name)</td>"
            
            // End the current row
            htmlStringBuilder += "<tr>"
        }
        
        // End html tag
        htmlStringBuilder += "</table>"
        
        let composer = MFMailComposeViewController()
        composer.mailComposeDelegate = self
        composer.setToRecipients(["sample@.com"])
        composer.setSubject("Porudzbina")
        
        // Add htmlStringBuilder builder as html
        composer.setMessageBody (htmlStringBuilder, isHTML: true)
        
        present(composer,animated: true)
    }
    

    The generated HTML from above code is actually like this

    <table border="1">
      <tr>
        <th style="padding:10px">Name</th>
        <th style="padding:10px">Packaging</th>
      </tr>
      <tr>
        <td>Test</td>
        <td>Test</td>
      </tr>
      <tr></tr>
      <tr>
        <td>Test 2</td>
        <td>Test 2</td>
      </tr>
      <tr></tr>
    </table>
    

    Final result gives you data organized in a table.

    MFMailComposeViewController HTML email data from UITableView in swift for iOS

    This is just basic HTML and you can do more styling and change how the output looks as you wish.