【问题标题】:Swift 3: How to name the PDF when emailing through iOS share sheet?Swift 3:通过 iOS 共享表发送电子邮件时如何命名 PDF?
【发布时间】: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
  • 很抱歉造成混淆,但该文件不是本地文件。它在我们公司的网站上。

标签: ios swift email pdf


【解决方案1】:

我使用的解决方法是将 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)
}

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 2018-09-19
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多