Swift 3:通过iOS共享表发送电子邮件时如何命名PDF?
上下文: 我有一个应用程序,可以查看设备规格表。现在,每个表视图都有一个“视图”和“共享”按钮,供用户与规范表的PDF版本交互。“查看”按钮在web视图中打开PDF,而“电子邮件”按钮将PDF添加为附件-这两种方法都很有效。我想不需要两个按钮,只需要一张共享表。共享表可以工作,但我不再以编程方式设置pdf的名称 问题:如何以与MailComposeViewController类似的方式,通过UIActivityViewController为邮件中附加的文件设置文件名?此外,是否可以在UIActivityViewController中以编程方式抄送组电子邮件 更新:该文件位于我们公司的网站上。应用程序的唯一属性是文件的URL位置。一切正常,我只想控制共享表打开的邮件生成器的输入 MailComposeViewController(按预期工作) UIActivityViewController(工作,但不命名文件)Swift 3:通过iOS共享表发送电子邮件时如何命名PDF?,ios,swift,email,pdf,Ios,Swift,Email,Pdf,上下文: 我有一个应用程序,可以查看设备规格表。现在,每个表视图都有一个“视图”和“共享”按钮,供用户与规范表的PDF版本交互。“查看”按钮在web视图中打开PDF,而“电子邮件”按钮将PDF添加为附件-这两种方法都很有效。我想不需要两个按钮,只需要一张共享表。共享表可以工作,但我不再以编程方式设置pdf的名称 问题:如何以与MailComposeViewController类似的方式,通过UIActivityViewController为邮件中附加的文件设置文件名?此外,是否可以在UIActi
我使用的解决方法是将UIAlertController与我希望用户拥有的选项一起使用。这使我可以选择运行发送电子邮件的功能
@IBAction func shareSpecButton(_ sender: UIBarButtonItem) {
if let contentURL = URL(string: targetURL) {
sendMailViaActionSheet(fileURL: contentURL)
}
}
private func sendMail (fileURL: URL) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
//composeVC.setToRecipients(["user@domain.com"])
composeVC.setCcRecipients(["group@domain.com"])
composeVC.setSubject("Spec Sheet Request")
composeVC.setMessageBody("The requested spec sheet is attached.", isHTML: false)
if let fileData = NSData(contentsOf: fileURL 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)
}
警报控制器设置:
private func sendMailViaActionSheet (fileURL: URL) {
// Set up Alert Controller
var alert = UIAlertController(
title: "Share Spec Sheet",
message: "Choose a method to share the spec sheet ",
preferredStyle: .actionSheet
)
// First Action
alert.addAction(UIAlertAction(
title: "Send pdf in an email",
style: UIAlertActionStyle.default)
{ (action: UIAlertAction) -> Void in
// code to execute when action is chosen
// Send pdf as data file
self.sendMail(fileURL: fileURL)
}
)
// Second Action
alert.addAction(UIAlertAction(
title: "Send link in an email",
style: UIAlertActionStyle.default)
{ (action: UIAlertAction) -> Void in
// code to execute when action is chosen
}
)
// Cancel Action
alert.addAction(UIAlertAction(
title: "Cancel",
style: .cancel)
{ (action: UIAlertAction) -> Void in
// do nothing
}
)
// Present the action sheet
present( alert, animated: true, completion: nil)
}
尝试将
sharedContent
设置为[contentURL]
而不是[content]
。换句话说,共享URL而不是数据。我一开始是这样做的,它在邮寄时不会附加PDF。当前方法附加PDF,但我无法控制附件的名称。或电子邮件主题。或收件人。如果它是本地文件URL,则应作为URL。不相关,但为什么在Swift中使用NSData
?只需使用数据
。很抱歉造成混淆,但该文件不是本地文件。它在我们公司的网站上。
@IBAction func shareSpecButton(_ sender: UIBarButtonItem) {
if let contentURL = URL(string: targetURL) {
sendMailViaActionSheet(fileURL: contentURL)
}
}
private func sendMail (fileURL: URL) {
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
// Configure the fields of the interface.
//composeVC.setToRecipients(["user@domain.com"])
composeVC.setCcRecipients(["group@domain.com"])
composeVC.setSubject("Spec Sheet Request")
composeVC.setMessageBody("The requested spec sheet is attached.", isHTML: false)
if let fileData = NSData(contentsOf: fileURL 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)
}
private func sendMailViaActionSheet (fileURL: URL) {
// Set up Alert Controller
var alert = UIAlertController(
title: "Share Spec Sheet",
message: "Choose a method to share the spec sheet ",
preferredStyle: .actionSheet
)
// First Action
alert.addAction(UIAlertAction(
title: "Send pdf in an email",
style: UIAlertActionStyle.default)
{ (action: UIAlertAction) -> Void in
// code to execute when action is chosen
// Send pdf as data file
self.sendMail(fileURL: fileURL)
}
)
// Second Action
alert.addAction(UIAlertAction(
title: "Send link in an email",
style: UIAlertActionStyle.default)
{ (action: UIAlertAction) -> Void in
// code to execute when action is chosen
}
)
// Cancel Action
alert.addAction(UIAlertAction(
title: "Cancel",
style: .cancel)
{ (action: UIAlertAction) -> Void in
// do nothing
}
)
// Present the action sheet
present( alert, animated: true, completion: nil)
}