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.

14
  • are you using swift 2 or swift 3? Commented Oct 20, 2016 at 13:41
  • This is Swift 3 now Commented Oct 20, 2016 at 13:44
  • @John Previously is it working? On swift 2 Commented Oct 20, 2016 at 13:49
  • 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? Commented Oct 20, 2016 at 13:52
  • 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. Commented Oct 20, 2016 at 13:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad you got it solved. On to the next problem then :)

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.