I have a WKWebView
in a view controller. When an user clicks on "Upload File" button (which is on the webpage shown), UIDocumentPickerViewController
pops up. This is expected and totally neccessary but:
Whenever the user clicks on any button ("Upload Photo or Video", "Cancel"), the UIDocumentPickerViewController
dismisses itself AND the parent view controller that it's in.
I have added a symbolic breakpoint for [UIViewController dismissViewControllerAnimated:completion:]
and indeed saw that -dismissViewController...
is called twice. After the first time it dismisses UIDocumentPickerViewController
, after the second one – my parent view controller.
By the way, on the iPad there is no problem, probably because UIDocumentPickerViewController
is presented as a popover.
Why is this happening and what should I do?
Thanks!
I faced the same problem and finally managed to find a workaround.
In my view controller I override
dismiss
method to delete managed object if save button wasn't hit and that worked perfectly withUIImagePickerController
.As of my application uses
UIDocumentPickerViewController
, my managed object was deleted each time thedocumentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
was called because this causes apresentingViewController.dismiss
.So my solution is to check from the
presentingViewController
if thepresentedViewController
isnil
or not to know ifdismiss
method was called byUIDocumentPickerViewController
or not.So here is my overriden
dismiss
method from my view controller.Hope it helps you.
Try this(working):
Alright, I think I've found a way to fix this. WebKit is open source, and you can see the offending class isWKFileUploadPanel
, specifically the_dismissDisplayAnimated:
method is called too aggressively inadvertently triggering dismissal of your view controller. In order to avoid this you need to block calls to-dismissViewControllerAnimated:completion:
that come in fromWKFileUploadPanel
, which you can do by looking up the stack. Here's an implementation that resolves this.The solution mentioned above doesn't work because it relies on looking at the call stack symbols for a particular class which is obfuscated when running on a real device. Here's a different approach I took to resolve this:
weak
UIDocumentMenuViewController
property to your modal view controller class.-presentViewController:animated:completion:
in your modal class to check and see if the view controller being presented is aUIDocumentMenuViewController
, if so set it as the value for your weak property from step 1.-dismissViewControllerAnimated:completion:
to check if your weak property isnil
yet, if it is notnil
and your modal'spresentedViewController
isnil
then it means that WebKit is trying to dismiss your modal when it shouldn't be. You can avoid callingsuper
in that case, and continue calling it otherwise.You can also swizzle
WKFileUploadPanel
's_dismissDisplayAnimated:
method to be more careful about your own view controllers, but that comes with a significant amount of risk.I have faced the same issue , This happens while using document picker and occurs in iOS versions below 11.4 . Use the below code where ever you are using document picker. From what I have read from different forums there is an issue with document picker and it has been fixed in later versions of iOS .
Declare a weak property of document picker .
Then implement the view controller delegate methods :