Swift iOS导出CSV文件 - 数据编码问题

时间:2016-06-08 14:00:51

标签: ios swift csv encoding utf

尝试从我的iOS应用程序(Xcode 7中的swift 2.0)中的数据创建导出CSV时,我遇到了一个有趣的问题。以下是失败模式的描述。对此的任何帮助将不胜感激!

当使用UTF8字符串编码并发送电子邮件时,我的CSV附件在从webmail打开时完全打开(即gmail,yahoo mail等使用浏览器:chrome,safari,firefox等)。但是,当通过Microsoft Outlook中设置的电子邮件帐户打开CSV附件时,附件变得毫无意义:

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

当我将编码更改为UTF16时,现在在Microsoft Outlook中设置的电子邮件帐户下载并完全打开附件;然而,网络邮件帐户(通过浏览器发送电子邮件)下载并打开附件中的正确信息,但看起来它没有被视为CSV并且所有内容都在一列中:

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,
1   0.0, 6.0,
Waste Amount:
0

(到处都有逗号应该在新列中)

密码代码块:

    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)

感谢您的帮助!

0 个答案:

没有答案