Exporting CSV file from iOS App - Data Encoding Issue

Hi all,

I am experiencing an interesting issue when attempting to create an export a CSV from data in my iOS app (swift 2.0 in Xcode 7). Below is a description of the pattern of failure. Any assistance with this would be greatly appreciated!


When using UTF8 String Encoding, and sending out the email, my CSV attachment opens perfectly when opened from webmail (i.e. gmail, yahoo mail etc using a browser: chrome, safari, firefox, etc). However when opening the CSV attachment through an email account set up in Microsoft Outlook, the attachment becomes nonsensical:


ezW

w JiHbcZ`..7'5*^y^tSCE*^y^ySE'

[ ‑+l!sk![ 6*'z

h:wl~h'Ym

g "im1*^

{‑r ]0:0‑X&{T-j;S

g# m

g5 wS

g# m

g6JV+58&x:0‑X&{a'u8&{mxe*^y^tSD-j;R%ggM4JZ)SNtY+-x (:{t


When i change the encoding to UTF16, now the email accounts set up in Microsoft Outlook download and open the attachment perfectly; however the webmail accounts (email through browser) download and open the attachment with the correct information, however it looks like it wasn't treated as a CSV and everything is in one column:


Study Name Results


Study Parameters:

Machine Type,Machine Name

Current Speed,500.0

Max Speed,750.0

Units, Units of Production (e.g pounds) / min

All time counts are in seconds

Downtime 2 Start Time, Downtime 2 End Time ,

2, 6,

Speed: 500.0 Start, Speed: 500.0 Stop,

0.0, 6.0,

Waste Amount:

0


(everywhere there is a comma should be in a new column)



Key block of code:


let csvString = generateExportString()

let data = csvString.dataUsingEncoding(NSUTF8StringEncoding) <--- this is where i've been changing the type of string encoding used

let mailComposer = MFMailComposeViewController()

mailComposer.mailComposeDelegate = self

if (MFMailComposeViewController.canSendMail()) {

mailComposer.setSubject("Stroud Line Study - \(study!.name)-Results")

mailComposer.setMessageBody("Stroud Line Study Results are attached", isHTML: false)

mailComposer.addAttachmentData(data!, mimeType: "text/csv", fileName: "\(study!.name)-Results.csv") <-- not sure if this is the best mimeType

}

self.presentViewController(mailComposer, animated: true, completion: nil)



Thank you for all your help in advance!

Oooh, there’s an RFC for CSV; didn’t know that.

The problem here is that CSV does not define a default text encoding and different mail agents use different defaults. You might be able to solve this problem by adding a

charset
parameter to the email but, alas, MFMailComposeViewController does not give you a way to include MIME type parameters. That’d make a fine enhancement request though.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 13 Jun through to Fri, 17 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

Hi Quinn,


Thanks for your response. I'll go ahead and submit an enhancement request; however in the mean time, for my users of the app, is there a potential work around such that they will all be able to receive their data correctly, regardless of mail agent default text encodings?


Thanks!

Have you considered creating and handing off a zip file instead of csv?

… in the mean time, for my users of the app, is there a potential work around such that they will all be able to receive their data correctly, regardless of mail agent default text encodings?

There are two parts to this:

  • What sort of metadata do the relevant mail agents require in order to display the CSV correctly?

  • How can you generate that metadata?

The first part is something that you’ll have to answer. You’ve already done a first pass at this, that is, working out the default behaviour of the relevant mail agents. You’ll now need to do some more in-depth research to figure out how to escape that default. For example:

  • does the

    charset
    parameter help?
  • does, as KMT suggesting, putting it in a zip file help?

Once you know what each mail agent requires, you can then ask about how to generate that metadata on the sender.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

WWDC runs Mon, 13 Jun through to Fri, 17 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/

Exporting CSV file from iOS App - Data Encoding Issue
 
 
Q