【发布时间】:2017-09-18 21:23:10
【问题描述】:
上下文: 我有一个带有设备规格表视图的应用程序。现在,每个表格视图都有一个“查看”和“共享”按钮,供用户与 PDF 版本的规格表进行交互。查看按钮在 Web 视图中打开 PDF,电子邮件按钮将 pdf 添加为附件 - 两者都很好用。我想消除对两个按钮的需求,只需要一个共享表。共享表有效,但我不再以编程方式设置 pdf 的名称。
问题:如何通过 UIActivityViewController 以类似于 MailComposeViewController 的方式为邮件中附加的文件设置文件名?另外,是否可以在 UIActivityViewController 中以编程方式抄送群组电子邮件?
更新:该文件位于我们公司的网站上。应用程序拥有的唯一属性是文件的 URL 位置。一切正常,我只想控制共享表打开的邮件编辑器的输入。
MailComposeViewController(按预期工作)
@IBAction func getSpecURLfromButton(_ sender: subclassedUIButton) {
// Get the URL from the button
buttonURL = sender.urlString!
// Now Send the Email
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
//composeVC.setToRecipients(["name@doamin.com"])
composeVC.setCcRecipients(["group@domain.com"])
composeVC.setSubject("Spec Sheet Request")
composeVC.setMessageBody("The requested spec sheet is attached.", isHTML: false)
// Attach the spec sheet at button URL to the email
// source: https://stackoverflow.com/questions/30423583/attach-a-pdf-file-to-email-swift
let specURL = URL(string: buttonURL)
if let fileData = NSData(contentsOf: specURL! as URL) {
print("File data loaded.")
composeVC.addAttachmentData( fileData as Data, mimeType: "application/pdf", fileName: "Tubular Spec Sheet")
}
//}
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
UIActivityViewController(有效,但不能命名文件)
@IBAction func shareSpecButton(_ sender: UIBarButtonItem) {
if let contentURL = URL(string: targetURL) {
let content = NSData(contentsOf: contentURL)
let sharedContent = [content]
let activityViewController = UIActivityViewController(activityItems: sharedContent, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}
}
【问题讨论】:
-
尝试将
sharedContent设置为[contentURL]而不是[content]。换句话说,共享 URL 而不是数据。 -
我一开始是这样做的,邮寄时它不会附加PDF。当前方法附加 PDF,但我无法控制附件的名称。或电子邮件主题。或收件人。
-
如果是本地文件 URL,它应该作为 URL 工作。
-
不相关,但你为什么在 Swift 中使用
NSData?只需使用Data。 -
很抱歉造成混淆,但该文件不是本地文件。它在我们公司的网站上。