Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 518
Akshay Yerneni Avatar asked Nov 18 '22 05:11

Akshay Yerneni


1 Answers

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)
   }
}
like image 147
wammy Avatar answered Jan 18 '23 16:01

wammy