ios - swift 3 : How to name the PDF when emailing through iOS share sheet?

标签 ios swift email pdf

上下文: 我有一个带有设备规范 TableView 的应用程序。现在,每个 TableView 都有一个“查看”和“共享”按钮,供用户与规范表的 PDF 版本进行交互。查看按钮在 Web View 中打开 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)
    }
}

最佳答案

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


}

警报 Controller 设置:

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)
}

关于ios - swift 3 : How to name the PDF when emailing through iOS share sheet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46288423/

相关文章:

ios - 转换成json后如何json列表

ios - 在 Swift 3 中启用核心数据轻量级迁移

Xcode:LLVM 7.0:禁用警告 "Umbrella header for module does not include header"

PHP 获取当前登录用户的邮箱

java - 从用户名outlook java获取电子邮件地址

objective-c - 在后台接收蓝牙管理器通知

ios - 未通过 updateViewConstraints 添加约束

swift - 正确使用 withMemoryRebound

python - 使用 python 在 Lotus Notes 中发送邮件

ios - 设置字符串/.text 的最大长度