我有一个PDF文件,我想在UIActivityType.markupAsPDF自动打开这意味着用户不需要搜索标记只需要进入标记按钮
下面是我的代码:if let pdfUrl = Bundle.main.url(forResource: "MyPDF", withExtension: "pdf") {
let activityController = UIActivityViewController(activityItems: [pdfUrl], applicationActivities:nil)
activityController.excludedActivityTypes = [
UIActivityType.postToFacebook,
UIActivityType.postToTwitter,
UIActivityType.postToWeibo,
UIActivityType.message,
UIActivityType.mail,
UIActivityType.copyToPasteboard,
UIActivityType.assignToContact,
UIActivityType.saveToCameraRoll,
UIActivityType.addToReadingList,
UIActivityType.postToFlickr,
UIActivityType.postToVimeo,
UIActivityType.postToTencentWeibo,
UIActivityType.airDrop,
UIActivityType.openInIBooks,
//UIActivityType.print,
]
// These activity types shouldn't be excluded
// UIActivityType.print,
// UIActivityType.markupAsPDF
self.present(activityController, animated: true, completion: nil)
}
在这段代码中,用户再次看到菜单,但几乎没有选择,我希望用户完全进入markupAsPDF (Markup) = Pencilkit
对于标记功能,您必须使用QLPreviewController
。
这是包装好的QLPreviewController
类。
import QuickLook
import PDFKit
class PDFMarkup: QLPreviewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate {
private var fileURL: URL?
override func viewDidLoad() {
self.navigationItem.title = ""
self.navigationController?.navigationBar.tintColor = nil
}
deinit {
debugPrint("‼️‼️‼️ deinit : (self) ‼️‼️‼️")
}
func openPDFDocument(with vc: UIViewController, documentURL: URL?) {
guard let documentURL = documentURL else {
print("Cannot open document because it's nil")
return
}
if let navController = vc.navigationController {
navController.present(self, animated: true, completion: nil)
// navController.pushViewController(self, animated: true)
} else {
vc.show(self, sender: nil)
}
if QLPreviewController.canPreview(documentURL as QLPreviewItem) {
self.fileURL = documentURL
self.currentPreviewItemIndex = 0
self.dataSource = self
self.delegate = self
self.setEditing(true, animated: true)
self.reloadData()
} else {
print("Item not supported by QLPreviewController")
}
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
self.navigationItem.title = ""
self.title = ""
return fileURL! as QLPreviewItem
}
func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
return .updateContents
}
func previewControllerDidDismiss(_ controller: QLPreviewController) {
}
func previewController(_ controller: QLPreviewController, didUpdateContentsOf previewItem: QLPreviewItem) {
}
}
使用
private let docPreviewer = PDFMarkup()
override func viewDidLoad() {
super.viewDidLoad()
self.docPreviewer.openPDFDocument(with: self, documentURL: /*your document url*/)
}