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.

Akshay Yerneni
  • 103
  • 2
  • 11
  • Which swift version are you using?. The code in the question works fine for me with Swift 3. – Ratnesh Jain Aug 15 '18 at 07: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. – Akshay Yerneni Aug 15 '18 at 09: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... – Thomas Deniau Aug 16 '18 at 09:03
  • @ThomasDeniau this isn't working either – Akshay Yerneni Aug 18 '18 at 04:45
  • Uhm. Who’s retaining your controller? Isn’t It getting dealloced as soon as the presentation ends, and not getting a chance to call its delegate? Try making it an ivar? – Thomas Deniau Aug 19 '18 at 06:26
  • 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!! – user2606782 Jan 17 '19 at 09:36

2 Answers2

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)
   }
}
wammy
  • 53
  • 1
  • 5
-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
Matija
  • 79
  • 3