UIActivityViewController发送带附件的电子邮件

时间:2015-02-26 15:36:23

标签: ios email swift csv

创建了一个UIActivityViewController来共享我的CSV文件。

我可以发送电子邮件,但我无法添加附件csv文件

这是我的代码:

 if ((directorys) != nil) {
            // Path
            let directories:[String] = directorys!;
            let dictionary = directories[0];
            let plistfile = "bpmonitor.csv"
            let plistpath = dictionary.stringByAppendingPathComponent(plistfile);
            println("\(plistpath)")

            csvString.writeToFile(plistpath, atomically: true, encoding: NSUTF8StringEncoding, error: nil)

            let activityViewController = UIActivityViewController(
                activityItems: ["body", NSData.dataWithContentsOfMappedFile(plistpath) as NSData],
                applicationActivities: nil)


            activityViewController.setValue("Your email Subject", forKey: "subject")
            activityViewController.completionHandler = {(activityType, completed:Bool) in
                if !completed {
                    println("cancelled")
                    return
                }
                if activityType == UIActivityTypeMail {

                }
                if activityType == UIActivityTypePostToTwitter {
                    println("twitter")
                }

                if activityType == UIActivityTypeMail {
                    println("mail")
                }
            }

1 个答案:

答案 0 :(得分:2)

尝试添加文件网址作为附件:

let activityViewController = UIActivityViewController(
            activityItems: [NSURL(fileURLWithPath: plistpath)],
            applicationActivities: nil)