I am trying to initiate DocumentPicker in my app. I have declared:
var documentPicker: UIDocumentPickerViewController =
UIDocumentPickerViewController(documentTypes:
["public.text"], in: UIDocumentPickerMode.open)
documentPicker.delegate = self
documentPicker.modalPresentationStyle =
UIModalPresentationStyle.fullScreen
self.present(documentPicker, animated: true, completion: nil)
}
and for picked document:
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
if controller.documentPickerMode == UIDocumentPickerMode.exportToService {
//do some stuff
}
dismiss(animated: true, completion: nil)
}
However i am doing something wrong here. Whenever i choose a file or close the window I keep getting this error:
viewServiceDidTerminateWithError: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}.
I have tried looking for a solution but i couldn't find anything useful.
EDIT: I have included a couple of print("test") into the documentPicker function to see if it is working. However i get 0 response from the console. Maybe I have implemented documentPicker in a bad way?
I, too, get this error message but not all the time.
I noticed the documentPicker is deprecated. Try this:
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL urls: [URL]) {
if controller.documentPickerMode == UIDocumentPickerMode.exportToService {
//do some stuff
}
}
There's no need to have the dismiss statement. The picker is closed automatically.
Try this and see if it works.
I figured out that my reference to the document picker was a local variable that was going out of scope after the picker is displayed. I believe when the view is dismissed, the picker is garbage collected before the callback occurs. I added a strong reference to the picker and no longer get that error message. And the callbacks now work correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With