Marco Menardi
Marco Menardi

Reputation: 491

Sharing csv file through UIActivityViewController

I'm trying to share a csv file using UIActivityViewController.
I want to share both through email and other applications, like Telegram.
Through Telegram the file gets shared correctly, but using email, the email has no attachments.
Also, the csv file has no extension, should I set a MIME type? How?

 @IBAction func shareSheet(sender: AnyObject) {

    let firstActivityItem = "Hi, here is the csv file"

    //do i really need this? what for?
    let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!

    let csv : NSData! = NSData(contentsOfFile: NSTemporaryDirectory() + "export.csv")

    let activityViewController : UIActivityViewController = UIActivityViewController(
        activityItems: [firstActivityItem, secondActivityItem, csv], applicationActivities: nil)

    //set the email title
    activityViewController.setValue(firstAcxtivityItem, forKey: "subject")


    self.presentViewController(activityViewController, animated: true, completion: nil)
}

Upvotes: 0

Views: 1092

Answers (1)

BigIves
BigIves

Reputation: 91

This is the code I use to send a CSV via the mail composer

let csvString = "Your CSV String"
    let subject = "Subject of your email"
    let fileName = "CSV Filename.csv"

    let composeVC = MFMailComposeViewController()
    composeVC.mailComposeDelegate = self
    composeVC.setSubject(subject)

    if let csvData = csvString.data(using: String.Encoding.utf8) {
        composeVC.addAttachmentData(csvData, mimeType: "text/csv", fileName: fileName)
    }

    self.present(composeVC, animated: true, completion: nil)

Upvotes: 1

Related Questions