Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while closing the UIDocumentPicker

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?

like image 531
misi06 Avatar asked Aug 12 '26 22:08

misi06


2 Answers

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.

like image 74
Bevan Avatar answered Aug 14 '26 11:08

Bevan


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.

like image 22
user3630663 Avatar answered Aug 14 '26 11:08

user3630663



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!