我创建了通过电子邮件发送或打印文件的代码。
但我想添加更多选项,例如通过Gmail发送,将文件保存到iBooks,保存到Dropbox等。
我添加了两个需求:电子邮件和打印。 我想添加一个Gmail和几个选项。
以下是代码:
func actionMenuViewControllerShareDocument(_ actionMenuViewController: ActionMenuViewController) {
let mailComposeViewController = MFMailComposeViewController()
if let lastPathComponent = pdfDocument?.documentURL?.lastPathComponent,
let attachmentData = pdfDocument?.dataRepresentation() {
mailComposeViewController.addAttachmentData(attachmentData, mimeType: "application/pdf", fileName: lastPathComponent)
}
}
func actionMenuViewControllerPrintDocument(_ actionMenuViewController: ActionMenuViewController) {
let printInteractionController = UIPrintInteractionController.shared
printInteractionController.printingItem = pdfDocument?.dataRepresentation()
printInteractionController.present(animated: true, completionHandler: nil)
}
代码2
import UIKit
class ActionMenuViewController: UITableViewController {
weak var delegate: ActionMenuViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
tableView.isScrollEnabled = false
tableView.separatorInset = .zero
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
delegate?.actionMenuViewControllerShareDocument(self)
} else if indexPath.row == 1 {
delegate?.actionMenuViewControllerPrintDocument(self)
tableView.deselectRow(at: indexPath, animated: true)
}
}
protocol ActionMenuViewControllerDelegate: class {
func actionMenuViewControllerShareDocument(_ actionMenuViewController: ActionMenuViewController)
func actionMenuViewControllerPrintDocument(_ actionMenuViewController: ActionMenuViewController)
}
答案 0 :(得分:0)
尝试以下操作,您将获得多种选项,例如通过Gmail发送,将文件保存到iBooks,保存到Dropbox等,这些都是iPhone的支持。
假设您在文档目录中有.pdf文件。
var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory: String = paths[0]
let fullPath: String = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("\("FileName").pdf").absoluteString
let url = URL(fileURLWithPath: fullPath)
let activityItems = [url]
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
self.present(activityViewController, animated: true, completion: nil)