如何从 UIActivityViewController 打开一个项目 swift

how to open one item from UIActivityViewController swift

我有一个 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(标记)= Pencilkit

对于标记功能,您必须使用 QLPreviewController

这是包装好的QLPreviewControllerclass.

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