5

I have a document picker, but after selecting a document, the didPickDocumentAt part is never triggered. It was working before I updated swift, is there something that is different now?

func selectDocument(_ sender: UIButton!){
    let documentPickerVC = UIDocumentPickerViewController(documentTypes: ["org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"], in: UIDocumentPickerMode.import)
    documentPickerVC.delegate = self
    self.present(documentPickerVC, animated: true, completion: nil)
}

func documentPicker(_ documentPicker: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
    print("file picked: \(url)")
    documentPicker.dismiss(animated: true, completion: nil)
}

Nothing is 'failing' either, it just isn't calling that documentPicker method.

I have a similar one for selecting media and that one works fine...

func selectMedia(_ sender: UIButton!){
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
        picker.delegate = self
        picker.allowsEditing = false
        picker.mediaTypes = [kUTTypeMovie as String]
        self.present(picker, animated: true, completion: nil)
    }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[String: Any]) {
    let url = info[UIImagePickerControllerMediaURL] as! URL
    print("media picked: \(url)")
}

Edit: I just added the documentPickerWasCancelled method, and for some reason that is being called when I select a document. Note I am selecting a document from google drive, would that have an affect on anything?

Edit 2: Answered, uninstalled and reinstalled and it worked. See answer below. Thanks everyone for the suggestions.

John
  • 1,808
  • 7
  • 28
  • 57
  • are you using swift 2 or swift 3? – alexburtnik Oct 20 '16 at 13:41
  • This is Swift 3 now – John Oct 20 '16 at 13:44
  • @John Previously is it working? On swift 2 – Nirav D Oct 20 '16 at 13:49
  • Do you have `iCloud Documents` enabled in `Capabilities` section of project settings? – alexburtnik Oct 20 '16 at 13:52
  • Yes it was working, and then I converted to swift 3 and now it doesn't. The DocumentPicker opens and everything and I can browse my files, but when I select one, the other method is never triggered – John Oct 20 '16 at 13:52
  • 1
    A quick guess from me. Does your `ViewController` implement the `UIDocumentPickerDelegate` protocol? I can see that you have the `didPickDocumentAt` method there but have you added `UIDocumentPickerDelegate` either at your class declaration or in an extension? – pbodsk Oct 20 '16 at 13:52
  • @pbodsk yes I have `UIDocumentPickerDelegate` – John Oct 20 '16 at 13:53
  • @alexburtnik I have that set as well, I don't think the document picker would even present it self if it wasn't but I could be mistaken – John Oct 20 '16 at 13:54
  • 2
    another guess then :) The documentation names the delegate method like this: `func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)` whereas you have `documentPicker(_ documentPicker: UIDocumentPickerViewController, didPickDocumentAt url: URL)` (the parameter is named `controller` where you have called it `documentPicker`), don't know if it makes any difference. – pbodsk Oct 20 '16 at 13:58
  • @pbodsk I just saw that also and changed it, I let swift 'auto complete' the method for me, but still doesn't work. I also added the `documentPickerWasCancelled` method and it looks like that one is being triggered for some reason – John Oct 20 '16 at 14:00
  • I'm sorry...I have no further ideas then....hope you get this solved – pbodsk Oct 20 '16 at 14:13
  • @John Just a suggestion. Have you tried the same code but with other `documentTypes`? Is it still not working. lets say, with "public.data"? – alexburtnik Oct 20 '16 at 14:29
  • @alexburtnik I tried that also, and it still does the same thing. It very quickly shows that is about to load the file than closes. Maybe a connection problem? But I am able to access the web fine.. – John Oct 20 '16 at 14:35
  • This happened to me with file names that contained a space in them, it was really baffling because some would work and others wouldn't. – Asleepace Oct 12 '17 at 23:07

1 Answers1

1

The above code looks like it is correct. I uninstalled the google drive app (where I was getting files from) and reinstalled it and then it worked as expected. I also tried from dropbox and that worked as well.

Not sure what was making it fail before.

John
  • 1,808
  • 7
  • 28
  • 57