Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OSX swift发送带有附件的邮件_Swift_Macos_Email_Cocoa - Fatal编程技术网

OSX swift发送带有附件的邮件

OSX swift发送带有附件的邮件,swift,macos,email,cocoa,Swift,Macos,Email,Cocoa,我和斯威夫特一起为osx工作。 我有发送邮件的代码: let service = NSSharingService(named: NSSharingServiceNameComposeEmail)! service.recipients = ["abc@domain.de"] service.subject = "My Subject" service.perform(withItems: ["My Message"]) 但我想附上一份pdf文件。 我怎样才能实现它呢?这项工作 let em

我和斯威夫特一起为osx工作。 我有发送邮件的代码:

let service = NSSharingService(named: NSSharingServiceNameComposeEmail)!
service.recipients = ["abc@domain.de"]
service.subject = "My Subject"
service.perform(withItems: ["My Message"])
但我想附上一份pdf文件。 我怎样才能实现它呢?

这项工作

let email = "your email here"
let path = "/Users/myname/Desktop/report.txt"
let fileURL = URL(fileURLWithPath: path)

let sharingService = NSSharingService(named: NSSharingServiceNameComposeEmail)
sharingService?.recipients = [email] //could be more than one
sharingService?.subject = "subject"
let items: [Any] = ["see attachment", fileURL] //the interesting part, here you add body text as well as URL for the document you'd like to share

sharingService?.perform(withItems: items)