How Swift Generates a CSV File

Asked 2 years ago, Updated 2 years ago, 84 views

Make the selected data in the app a CSV file and
Call Mailer,
I would like to attach it to an email and send it to you.
I don't know what to do.

Even if I looked it up on my own, there was only a way to load the CSV file, so
I'm struggling.

If you don't mind, please let me know.
Thank you for your cooperation.

Add **************
What should I do if I want to attach two separate csv files?

swift iphone csv

2022-09-30 19:29

1 Answers

The class MFMailComposeViewController makes it easy to call iPhone mailers from the app.(You can also set up an attachment, so I think it's perfect for your question.)

 func sendMailWithCSV(subject: String, message: String, csv:[String]]) {
    letmailVC = MFMailComposeViewController()
    mailVC.mailComposeDelegate=self
    mailVC.setSubject(subject)
    mailVC.setMessageBody (message, isHTML:false)
    mailVC.addAttachmentData(toCSV(csv)
        .dataUsingEncoding (NSUTF8 StringEncoding, allowLossyConversion: false)
        , mimeType: "text/csv"
        , fileName: "data.csv")
    self.presentViewController(mailVC, animated:true){}
}

Also, toCSV which creates CSV strings from a multidimensional array of strings was written with reference to the following page.
http://ja.wikipedia.org/wiki/Comma-Separated_Values

func toCSV(a:[[String]]])->String{
    US>return join("\n", a.map {join(", ", $0.map {ein
        contains(e) {contains("\n\"", ", $0)}
            "\"" + e.stringByReplacingOccurrencesOfString("\"", withString:"\"\"", options:nil, range:nil)+"\":e
    })}) + "\n"
}


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.