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!!
The answer is simple: This is inherited from a UIViewController. If you just add the view of the viewController to your view, the delegate methods are not called. A ViewController has its own lifecycle. Please have a read here:https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontrollerSo, apologies for being some kind of wrong. For sure you can add a sub-viewController showing just its view. But: I think that shouldn't be the use-case. It is a full screen ViewController conforming to design guides from apple itself. That being said, you should present it with:
There are some bugs filed where developer discovered that the view is being dismissed before the delegate is called. As far as I have seen, this behavior was introduced with ios11 and occured also when the viewController was presented. I can't really say if this is fixed or not nor if this behavior is related to show it as a subview. (I think it is somehow fixed as it works with a presented viewController)
Anyway, you should just present it as written above and you are good to go.