iOS MFMailComposeViewController not attaching file












0















Summary:



I am using the below snippet for sending an email from my app to an email address.
Below implementation works fine in case of configuration as below :



iOS Device : Any



iOS Version : Below 12



However in case of iOS 12 and iPhone XR, I am not able to see attached file in mail.
This is reproducible only on iPhone XR with iOS 12+. In case of other devices having iOS 12 the issue is not reproducible.



func sendMail(filePath:String){

let toRecipients = user@domain.com
let subject = “Email Subject”

let formatter = DateFormatter()
formatter.dateFormat="dMMMyyyy";
let dateString = formatter.string(from: Date())

let userName :String = “ABC”
var bodyText1: String?
var bodyTextLocation: String?

bodyText1 = "<html><body><br> " + “Date:” + dateString
bodyText1 = bodyText1! + "<br><br>------------------------------<br>"
+ “Contact us for additional help” + "</body></html>"

let body = "<html><body><br><br><br><br><br>" + UIDevice.current.model + "<br> " + "</body></html>"
var bodyText2 : String?
bodyText2 = body + bodyText1!

let mail = configuredMailComposeViewController(recipients: toRecipients, subject: subject, body: bodyText2!, isHtml: true, images: nil)

if let fileData = try? Data(contentsOf: URL(fileURLWithPath: filePath))
{
print("File data loaded.")
let logFileName = (filePath as NSString).lastPathComponent
mail.addAttachmentData(fileData, mimeType: "text/plain", fileName: logFileName)
}
presentMailComposeViewController(mailComposeVC: mail)
}


func presentMailComposeViewController(mailComposeVC : MFMailComposeViewController) {

if MFMailComposeViewController.canSendMail(){
self.rootVC.present(mailComposeVC, animated: true, completion: nil)
}else{
//Error
}
}


Steps to Reproduce:



Use iOS 12+ and iPhone XR
Use above code snippet



Expected Results:



File should get attached in email body.



Actual Results:



File is not getting attached in email body.
Note : This gets reproduced randomly.



Version/Build:



iOS 12+



Configuration used:



iPhone XR
iOS 12+










share|improve this question

























  • Add breakpoint and check if you are getting fileData. Also check filePath.

    – Venk
    Jan 1 at 5:12











  • do you see the "File data loaded." line in your Xcode console log?

    – Michael Dautermann
    Jan 1 at 6:59
















0















Summary:



I am using the below snippet for sending an email from my app to an email address.
Below implementation works fine in case of configuration as below :



iOS Device : Any



iOS Version : Below 12



However in case of iOS 12 and iPhone XR, I am not able to see attached file in mail.
This is reproducible only on iPhone XR with iOS 12+. In case of other devices having iOS 12 the issue is not reproducible.



func sendMail(filePath:String){

let toRecipients = user@domain.com
let subject = “Email Subject”

let formatter = DateFormatter()
formatter.dateFormat="dMMMyyyy";
let dateString = formatter.string(from: Date())

let userName :String = “ABC”
var bodyText1: String?
var bodyTextLocation: String?

bodyText1 = "<html><body><br> " + “Date:” + dateString
bodyText1 = bodyText1! + "<br><br>------------------------------<br>"
+ “Contact us for additional help” + "</body></html>"

let body = "<html><body><br><br><br><br><br>" + UIDevice.current.model + "<br> " + "</body></html>"
var bodyText2 : String?
bodyText2 = body + bodyText1!

let mail = configuredMailComposeViewController(recipients: toRecipients, subject: subject, body: bodyText2!, isHtml: true, images: nil)

if let fileData = try? Data(contentsOf: URL(fileURLWithPath: filePath))
{
print("File data loaded.")
let logFileName = (filePath as NSString).lastPathComponent
mail.addAttachmentData(fileData, mimeType: "text/plain", fileName: logFileName)
}
presentMailComposeViewController(mailComposeVC: mail)
}


func presentMailComposeViewController(mailComposeVC : MFMailComposeViewController) {

if MFMailComposeViewController.canSendMail(){
self.rootVC.present(mailComposeVC, animated: true, completion: nil)
}else{
//Error
}
}


Steps to Reproduce:



Use iOS 12+ and iPhone XR
Use above code snippet



Expected Results:



File should get attached in email body.



Actual Results:



File is not getting attached in email body.
Note : This gets reproduced randomly.



Version/Build:



iOS 12+



Configuration used:



iPhone XR
iOS 12+










share|improve this question

























  • Add breakpoint and check if you are getting fileData. Also check filePath.

    – Venk
    Jan 1 at 5:12











  • do you see the "File data loaded." line in your Xcode console log?

    – Michael Dautermann
    Jan 1 at 6:59














0












0








0








Summary:



I am using the below snippet for sending an email from my app to an email address.
Below implementation works fine in case of configuration as below :



iOS Device : Any



iOS Version : Below 12



However in case of iOS 12 and iPhone XR, I am not able to see attached file in mail.
This is reproducible only on iPhone XR with iOS 12+. In case of other devices having iOS 12 the issue is not reproducible.



func sendMail(filePath:String){

let toRecipients = user@domain.com
let subject = “Email Subject”

let formatter = DateFormatter()
formatter.dateFormat="dMMMyyyy";
let dateString = formatter.string(from: Date())

let userName :String = “ABC”
var bodyText1: String?
var bodyTextLocation: String?

bodyText1 = "<html><body><br> " + “Date:” + dateString
bodyText1 = bodyText1! + "<br><br>------------------------------<br>"
+ “Contact us for additional help” + "</body></html>"

let body = "<html><body><br><br><br><br><br>" + UIDevice.current.model + "<br> " + "</body></html>"
var bodyText2 : String?
bodyText2 = body + bodyText1!

let mail = configuredMailComposeViewController(recipients: toRecipients, subject: subject, body: bodyText2!, isHtml: true, images: nil)

if let fileData = try? Data(contentsOf: URL(fileURLWithPath: filePath))
{
print("File data loaded.")
let logFileName = (filePath as NSString).lastPathComponent
mail.addAttachmentData(fileData, mimeType: "text/plain", fileName: logFileName)
}
presentMailComposeViewController(mailComposeVC: mail)
}


func presentMailComposeViewController(mailComposeVC : MFMailComposeViewController) {

if MFMailComposeViewController.canSendMail(){
self.rootVC.present(mailComposeVC, animated: true, completion: nil)
}else{
//Error
}
}


Steps to Reproduce:



Use iOS 12+ and iPhone XR
Use above code snippet



Expected Results:



File should get attached in email body.



Actual Results:



File is not getting attached in email body.
Note : This gets reproduced randomly.



Version/Build:



iOS 12+



Configuration used:



iPhone XR
iOS 12+










share|improve this question
















Summary:



I am using the below snippet for sending an email from my app to an email address.
Below implementation works fine in case of configuration as below :



iOS Device : Any



iOS Version : Below 12



However in case of iOS 12 and iPhone XR, I am not able to see attached file in mail.
This is reproducible only on iPhone XR with iOS 12+. In case of other devices having iOS 12 the issue is not reproducible.



func sendMail(filePath:String){

let toRecipients = user@domain.com
let subject = “Email Subject”

let formatter = DateFormatter()
formatter.dateFormat="dMMMyyyy";
let dateString = formatter.string(from: Date())

let userName :String = “ABC”
var bodyText1: String?
var bodyTextLocation: String?

bodyText1 = "<html><body><br> " + “Date:” + dateString
bodyText1 = bodyText1! + "<br><br>------------------------------<br>"
+ “Contact us for additional help” + "</body></html>"

let body = "<html><body><br><br><br><br><br>" + UIDevice.current.model + "<br> " + "</body></html>"
var bodyText2 : String?
bodyText2 = body + bodyText1!

let mail = configuredMailComposeViewController(recipients: toRecipients, subject: subject, body: bodyText2!, isHtml: true, images: nil)

if let fileData = try? Data(contentsOf: URL(fileURLWithPath: filePath))
{
print("File data loaded.")
let logFileName = (filePath as NSString).lastPathComponent
mail.addAttachmentData(fileData, mimeType: "text/plain", fileName: logFileName)
}
presentMailComposeViewController(mailComposeVC: mail)
}


func presentMailComposeViewController(mailComposeVC : MFMailComposeViewController) {

if MFMailComposeViewController.canSendMail(){
self.rootVC.present(mailComposeVC, animated: true, completion: nil)
}else{
//Error
}
}


Steps to Reproduce:



Use iOS 12+ and iPhone XR
Use above code snippet



Expected Results:



File should get attached in email body.



Actual Results:



File is not getting attached in email body.
Note : This gets reproduced randomly.



Version/Build:



iOS 12+



Configuration used:



iPhone XR
iOS 12+







ios swift mfmailcomposeviewcontroller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 6:27







etoUser

















asked Jan 1 at 5:10









etoUseretoUser

1




1













  • Add breakpoint and check if you are getting fileData. Also check filePath.

    – Venk
    Jan 1 at 5:12











  • do you see the "File data loaded." line in your Xcode console log?

    – Michael Dautermann
    Jan 1 at 6:59



















  • Add breakpoint and check if you are getting fileData. Also check filePath.

    – Venk
    Jan 1 at 5:12











  • do you see the "File data loaded." line in your Xcode console log?

    – Michael Dautermann
    Jan 1 at 6:59

















Add breakpoint and check if you are getting fileData. Also check filePath.

– Venk
Jan 1 at 5:12





Add breakpoint and check if you are getting fileData. Also check filePath.

– Venk
Jan 1 at 5:12













do you see the "File data loaded." line in your Xcode console log?

– Michael Dautermann
Jan 1 at 6:59





do you see the "File data loaded." line in your Xcode console log?

– Michael Dautermann
Jan 1 at 6:59












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993133%2fios-mfmailcomposeviewcontroller-not-attaching-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993133%2fios-mfmailcomposeviewcontroller-not-attaching-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith