New line in emailBody not working in swift
Asked Answered
D

2

6

I have this code for showing some content in emailMessageBody. The code is given below.

var emailViewController : MFMailComposeViewController = MFMailComposeViewController()
        emailViewController.mailComposeDelegate = self
        emailViewController.setToRecipients(["----"])
        emailViewController.setSubject("----")
        emailViewController.setMessageBody("\n\n\n" + "-- \r\n " + "----- Device : \(self.deviceName), App Version : \(self.appVersion), Model : \(deviceModel) , iOS Version : \(self.deviceVersion), Country Code : \(self.countryCode), Localised Model : \(deviceLocalizedModel) \n \n \n", isHTML: true)
        self.presentViewController(emailViewController, animated: true, completion: nil)

You guys see that i put "\n" in the content, but its not showing new lines in the emailBody. If anyone know how to get some new lines in emailMessageBody, Please help.

Disillusionize answered 17/11, 2014 at 13:30 Comment(3)
Use <\br>, because email body is in HTML format and in html you have to use <br> to break a line.Electrotherapeutics
@Kampai Oh God. How could i forget about that.//Disillusionize
You need to add a tag <br> this. Not \br.Electrotherapeutics
F
10

You need to use </br> instead of \n

So your example code will look like the following

var emailViewController : MFMailComposeViewController = MFMailComposeViewController()
    emailViewController.mailComposeDelegate = self
    emailViewController.setToRecipients(["----"])
    emailViewController.setSubject("----")
    emailViewController.setMessageBody("</br></br></br>" + "-- </br> " + "----- Device : \(self.deviceName), App Version : \(self.appVersion), Model : \(deviceModel) , iOS Version : \(self.deviceVersion), Country Code : \(self.countryCode), Localised Model : \(deviceLocalizedModel) </br></br></br>", isHTML: true)
    self.presentViewController(emailViewController, animated: true, completion: nil)
Flypaper answered 8/9, 2016 at 14:2 Comment(1)
This answer turned up in the low quality review queue, presumably because you don't provide any explanation of the code. If this code answers the question, consider adding adding some text explaining the code in your answer. This way, you are far more likely to get more upvotes — and help the questioner learn something new.Cher
B
6

If you want to use \n you have to set the parameter isHTML to false in the setMessageBody call, otherwise you must use </br> instead of \n when you set the content for the message body.

Benares answered 14/3, 2017 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.