How to set up multiple UIDocumentPickers in the same VC

169 Views Asked by At

In one VC I am working in there are three buttons, and each button should present a document picker for different types of media. I have this configured-- but what I am not sure how to do is to handle my delegate method

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

How can I monitor which document picker set off the delegate method, since every button should handle this function differently? I tried to use controller.tag, but this doesn't seem to be a property of a UIDocumentPickerViewController. Any pointers would be much appreciated thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

You can create 3 separate instances of UIDocumentPickerViewController in your view controller and check the instance of controller in delegate method.

private lazy var documentPicker1: UIDocumentPickerViewController = {
    let controller = UIDocumentPickerViewController()
    controller.delegate = self
    return controller
}()

private lazy var documentPicker2: UIDocumentPickerViewController = {
    let controller = UIDocumentPickerViewController()
    controller.delegate = self
    return controller
}()

private lazy var documentPicker3: UIDocumentPickerViewController = {
    let controller = UIDocumentPickerViewController()
    controller.delegate = self
    return controller
}()

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    
    if controller == documentPicker1 {

    } else if controller == documentPicker2 {

    }
    ...
}

Or you could keep a property in view controller to track which button was tapped and check it in the delegate method.