I have a webview in a modal view controller on iOS13. When the user tries to upload an image to the webview, it crashes.
This is the exception I'm getting:
2019-09-30 17:50:10.676940+0900 Engage[988:157733] * Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIDocumentMenuViewController (). In its current trait environment, the modalPresentationStyle of a UIDocumentMenuViewController with this style is UIModalPresentationPopover. You must provide location information for this popover through the view controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the view controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.' * First throw call stack: (0x18926c98c 0x188f950a4 0x18cb898a8 0x18cb939b4 0x18cb914f8 0x18d283b98 0x18d2737c0 0x18d2a3594 0x1891e9c48 0x1891e4b34 0x1891e5100 0x1891e48bc 0x193050328 0x18d27a6d4 0x1002e6de4 0x18906f460) libc++abi.dylib: terminating with uncaught exception of type NSException
I'm not sure where could I set this delegate...
I made a sample project: https://github.com/ntnmrndn/WKUploadFormCrash And filled a bug report to Apple


As @jshapy8 correctly stated, you need to override the
present()method and setting the.sourceView/.sourceFrame/.barButtonItemmanually. But you need to keep in mind that in case theUIViewControllerthat holds theWkWebViewis presented by aUINavigationController, theUINavigationControlleris responsible for presenting otherUIViewController.Unless you are on an iPad.
So in fact you need to override the
present()method in yourUINavigationControlleras well as in theUIViewControllerwhich holds theWkWebView.In the example below, the
UIViewControllerwhich holds theWkWebViewis calledWebVC.In your
UINavigationControlleryou need to add:And in your
WebVCyou need to add:So you can use the new iOS 13 modal presentation style and upload files without crashing
Edit: This crashing behavior seems to be (another) iOS 13 bug, because this is only a problem on iPhones not on iPads (just tested it on iPads with iOS 12 & 13. It kinda looks like the apple engineers simply forgot that in case the
WKWebViewis presented with their new modal presentation style, theUIDocumentMenuViewControlleris presented withUIModalPresentationPopoverstyle, even on phones, which was until iOS 13 simply not the case.I updated my code so now it sets the
.sourceView/.sourceFrame/.barButtonItemonly for phone types, because tablet types will be handled by iOS it self correctly.