Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDocumentPickerDelegate method(didPickDocumentsAt) not called by iOS

I have a query regarding delegate method not been getting called for DocumentPickerViewController, here's the background, I just need to import the resource whatever available from my Files App and for that reason i am using UIDocumentPickerViewController.

I have a separate ViewController to which i add documentPickerViewController's view as subview and add it's delegate. My ViewController's code goes like this.

var documentPickerController: UIDocumentPickerViewController!
  let supportedUTI = [kUTTypeImage,kUTTypeSpreadsheet,kUTTypePresentation,kUTTypeDatabase,kUTTypeFolder,kUTTypeZipArchive,kUTTypeVideo, kUTTypeAudiovisualContent]

documentPickerController = UIDocumentPickerViewController.init(documentTypes: supportedUTI as [String], in: .import)
    documentPickerController.delegate = self
    documentPickerController.allowsMultipleSelection = false
    view.addSubview(documentPickerController.view)

Now as i see pickercontroller is opened and when i tap on Cancel documentPickerWasCancelled is called but when i select a file documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL] is not called.

I tried to dip in further to my surprise what i see is instead of showing my ViewController to which i add picker's view as subview if i directly show pickerViewController like this

UIDocumentPickerViewController *dc = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:[self UTITypes] inMode:UIDocumentPickerModeImport];
    dc.delegate = self;
    [MainVC presentViewController:dc animated:YES completion:nil];

both the delegate method are called just fine. I don't understand why. Can someone please help me out here!! Thanks in advance!!

like image 818
user2606782 Avatar asked Nov 06 '22 21:11

user2606782


1 Answers

So I had the exact same issue, the documentPickerWasCancelled delegate method is called but the didPickDocumentsAt would not get called.

Also of note, when I moved all of the delegation/presentation logic into my base view controller the UIPickerDelegate methods worked as expected. This let me know that there weren't any configuration type issues blocking functionality.

I'm not exactly sure what the problem is but it seems that if the document picker is presented on a complex view hierarchy something breaks.

What I ended up doing to work around this issue was creating a new window and presenting the document picker there:

func showDocumentPicker() {

        let documentTypes = ["public.image", "com.adobe.pdf"]

        let picker = UIDocumentPickerViewController(documentTypes: documentTypes, in: .import)
        picker.delegate = self
        picker.allowsMultipleSelection = true
        picker.modalPresentationStyle = .formSheet

        let window = UIWindow(frame: UIScreen.main.bounds)
        let newWindowBaseVC = UIViewController()
        newWindowBaseVC.view.backgroundColor = UIColor.clear
        window.rootViewController = newWindowBaseVC
        window.windowLevel = UIWindow.Level.alert
        window.makeKeyAndVisible()
        newWindowBaseVC.present(picker, animated: false, completion: nil)
    }
like image 88
Leo Avatar answered Nov 15 '22 05:11

Leo