'Swift on Mac (designed for iPad): MailComposer sends double attachments

We use mailComposeController to send emails with attachments from our iOS app. This works correctly on iOS devices. On Mac devices running Monterey 12.2, email sends as intended and attachments appear correctly in the displayed mailComposeController. However, once sent, the recipient gets what appears to be an un-openable copy of the attachment, along with the correct attachment. Extract of code, below:

        if MFMailComposeViewController.canSendMail() {
            
            let mailComposer = MFMailComposeViewController()
            mailComposer.mailComposeDelegate = self
            
            mailComposer.setToRecipients(recipients)

            mailComposer.setMessageBody(body, isHTML: true)

            mailComposer.setSubject(subject)
   
            mailComposer.addAttachmentData(theData!, mimeType:(theMIMEType?.rawValue) ?? ".pdf", fileName: fileName)
           
            present(mailComposer, animated: true, completion: nil)
        }

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

    //code here handles result of send attempt
}

As noted above, code runs correctly on iOS devices, but generates "ghost" image of attachment in sent email on Mac devices running on Apple Silicon. Thoughts much appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source