Context: I have an app with table view for equipment specifications. Right now, each table view has a "view" and "share" button for the user to interact with the PDF version of the spec sheet. The view button opens the PDF in a web view, the email button adds the pdf as an attachment- both work great. I wanted to remove the need for two buttons and simply have a share sheet. The share sheet works, but I am no longer programmatically setting the pdf's name.
Question: How to set a filename for files attached in mail via UIActivityViewController in a similar way to MailComposeViewController? Also, is it possible to programmatically CC the group email in UIActivityViewController?
Update: The file is located on our company's website. The only property that the app has is the URL location of the file. Everything works, I just want to control the inputs to the mail composer that the share sheet opens.
MailComposeViewController (working as expected)
@IBAction func getSpecURLfromButton(_ sender: subclassedUIButton) {
// Get the URL from the button
buttonURL = sender.urlString!
// Now Send the Email
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
//composeVC.setToRecipients(["name@doamin.com"])
composeVC.setCcRecipients(["group@domain.com"])
composeVC.setSubject("Spec Sheet Request")
composeVC.setMessageBody("The requested spec sheet is attached.", isHTML: false)
// Attach the spec sheet at button URL to the email
// source: https://stackoverflow.com/questions/30423583/attach-a-pdf-file-to-email-swift
let specURL = URL(string: buttonURL)
if let fileData = NSData(contentsOf: specURL! as URL) {
print("File data loaded.")
composeVC.addAttachmentData( fileData as Data, mimeType: "application/pdf", fileName: "Tubular Spec Sheet")
}
//}
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
UIActivityViewController (works, but not naming file)
@IBAction func shareSpecButton(_ sender: UIBarButtonItem) {
if let contentURL = URL(string: targetURL) {
let content = NSData(contentsOf: contentURL)
let sharedContent = [content]
let activityViewController = UIActivityViewController(activityItems: sharedContent, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}
}