6

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.

6
  • Which swift version are you using?. The code in the question works fine for me with Swift 3. Commented Aug 15, 2018 at 7:31
  • I'm using Swift 3 too. The same code is working fine if I'm using it in a new project. But somehow it isn't working it in this particular project I'm working on. Commented Aug 15, 2018 at 9:34
  • Can you try adding the override keyword or a @objc attribute with the right name? I’m wondering if the compiler is able to infer it automatically... Commented Aug 16, 2018 at 9:03
  • @ThomasDeniau this isn't working either Commented Aug 18, 2018 at 4:45
  • 2
    @AkshayYerneni - anything on this!! I am getting the similar issue... delegate method didPickDocumentsAt not getting called at all, although cancelled callback is working. Thanks!! Commented Jan 17, 2019 at 9:36

2 Answers 2

1

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)
   }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.