iosswiftuidocumentinteraction

How to use UIDocumentInteractionController?


I have created a program that loads PDF files. I want when the user is editing that they can publish the file anywhere in PDF format.

Do I use UIDocumentInteractionController or use UIActivityViewController?

Here is the code:

import UIKit
import PDFKit

@available(iOS 11.0, *)
@available(iOS 11.0, *)

class PDFViewControllerEN: UIViewController {
    var document: UIDocumentInteractionController!

    override func viewDidLoad() {
        super.viewDidLoad()
        // retrieve URL to file in main bundle`
    }

    @IBOutlet var pdfview: UIView!

    @IBAction func share(_ sender: UIButton) {
    }

    @IBAction func doAction2(_ sender: UIBarButtonItem) {
        document.presentOptionsMenu(from: view.bounds, in: view, animated: true)
    }

    override func viewWillAppear(_ animated: Bool) {
        //Here you are going to display your PdfController
        //PDFController that is seprate class you had created to show pdf file being opened
        //i.e

        //check which button was being selected
        switch ButtonSelected.Tag {
        case 0:
            var document: UIDocumentInteractionController = {
                let pdfView = PDFView(frame: UIScreen.main.bounds)

                let url = Bundle.main.url(forResource: "EN1", withExtension: "pdf")

                let vc = UIDocumentInteractionController(url: url!)
                pdfView.document = PDFDocument(url: url!)
                view.addSubview(pdfView)
                vc.delegate = self

                return vc
            }()
            //  document.presentPreview(animated: true)
            break
        case 1:
            //here control when you selected button with tag 0
            //here need to open pdf AR2
            //set Frame here all bounds

            var document: UIDocumentInteractionController = {
                let pdfView = PDFView(frame: UIScreen.main.bounds)

                let url = Bundle.main.url(forResource: "EN2", withExtension: "pdf")

                let vc = UIDocumentInteractionController(url: url!)
                pdfView.document = PDFDocument(url: url!)
                view.addSubview(pdfView)
                vc.delegate = self

                return vc
            }()
            break
        case 2:
            //here control when you selected button with tag 0
            //here need to open pdf AR2
            //set Frame here all bounds

            var document: UIDocumentInteractionController = {
                let pdfView = PDFView(frame: UIScreen.main.bounds)

                let url = Bundle.main.url(forResource: "EN3", withExtension: "pdf")

                let vc = UIDocumentInteractionController(url: url!)
                pdfView.document = PDFDocument(url: url!)
                view.addSubview(pdfView)
                vc.delegate = self

                return vc
            }()
            break
        default:
            //Error Case
            print("No tag Value Available")
        }
    }
}

@available(iOS 11.0, *)
extension PDFViewControllerEN: UIDocumentInteractionControllerDelegate {
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }
}

Solution

  • Using UIDocumentInteractionController is quite easy. You just need to know the url of your file, then you present the menu:

    /// Needs to be global, otherwise the controller will be destroyed when the file is handed over to target application
    var documentInteractionController: UIDocumentInteractionController!
    
    class MyViewController: UIViewController {
    
        var url: URL
    
        ...
    
        @IBAction func share(_ sender: UIBarButtonItem) {
            documentInteractionController = UIDocumentInteractionController()
            documentInteractionController.url = url
            documentInteractionController.uti = url.uti
            documentInteractionController.presentOptionsMenu(from: sender, animated: true)
        }
    
    }
    
    extension URL {
    
        var uti: String {
            return (try? self.resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier ?? "public.data"
        }
    
    }