Akshay Yerneni
Akshay Yerneni

Reputation: 103

UIDocumentPickerViewController - Delegate method not being called

I'm using UIDocumentPickerViewController to select documents from the Files and upload it to a server. I'm able to successfully access Files, but upon clicking on the file the delegate method doesn't get called.

I've used the following code to call the document picker:

class Uploads: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func uploadDocument(_ sender: Any) {

        let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypePlainText)], in: .import)
        documentPicker.delegate = self
        if #available(iOS 11.0, *) {
            documentPicker.allowsMultipleSelection = false
        } else {
        }
        present(documentPicker, animated: true, completion: nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension Uploads: UIDocumentPickerDelegate {

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

        print(urls.first)
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("Cancelled")
    }
}

I noticed that I'm getting the following warning upon calling the delegate method:

Instance method 'documentPicker(:didPickDocumentsAt:)' nearly matches optional requirement 'documentPicker(:didPickDocumentsAt:)' of protocol 'UIDocumentPickerDelegate'

Make 'documentPicker(_:didPickDocumentsAt:)' private to silence this warning

I believe that the delegate method isn't being called due to this warning, although I couldn't figure out why I'm getting this warning.

Upvotes: 6

Views: 2770

Answers (2)

wammy
wammy

Reputation: 53

Sharing code sample hopefully it'll help:

class ViewController : UIViewController,UIDocumentPickerDelegate{

  var documentBrowser: UIDocumentPickerViewController = {
  let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
  let browser = UIDocumentPickerViewController(documentTypes: [documentsPath], in: .open)
      browser.allowsMultipleSelection = true
      return browser
    }()

override func viewDidLoad() {
        super.viewDidLoad()
        self.addChild(documentBrowser)
        documentBrowser.view.frame = self.view.bounds
        view.addSubview(documentBrowser.view)
 }

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

Upvotes: 1

Matija
Matija

Reputation: 114

Problem appears if class which adopts 'UIDocumentPickerDelegate' protocol is declared as 'open'.

For example this class will have a problem:

open class FilePickerHelper: UIDocumentPickerDelegate

while this class will not have a problem:

class FilePickerHelper: UIDocumentPickerDelegate

Upvotes: -1

Related Questions