WKWebview with the new iOS13 modal crash when a file picker is invoked

As @jshapy8 correctly stated, you need to override the present() method and setting the .sourceView/.sourceFrame/.barButtonItem manually. But you need to keep in mind that in case the UIViewController that holds the WkWebView is presented by a UINavigationController, the UINavigationController is responsible for presenting other UIViewController.

Unless you are on an iPad.

So in fact you need to override the present() method in your UINavigationController as well as in the UIViewController which holds the WkWebView.

In the example below, the UIViewController which holds the WkWebView is called WebVC.

In your UINavigationController you need to add:

  override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
    if let webVC = viewControllers.filter({ $0 is WebVC }).first as? WebVC {
      webVC.setUIDocumentMenuViewControllerSoureViewsIfNeeded(viewControllerToPresent)
    }
    super.present(viewControllerToPresent, animated: flag, completion: completion)
  }

And in your WebVC you need to add:

  override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
    setUIDocumentMenuViewControllerSoureViewsIfNeeded(viewControllerToPresent)
    super.present(viewControllerToPresent, animated: flag, completion: completion)
  }

  func setUIDocumentMenuViewControllerSoureViewsIfNeeded(_ viewControllerToPresent: UIViewController) {
    if #available(iOS 13, *), viewControllerToPresent is UIDocumentMenuViewController && UIDevice.current.userInterfaceIdiom == .phone {
      // Prevent the app from crashing if the WKWebView decides to present a UIDocumentMenuViewController while it self is presented modally.
      viewControllerToPresent.popoverPresentationController?.sourceView = webView
      viewControllerToPresent.popoverPresentationController?.sourceRect = CGRect(x: webView.center.x, y: webView.center.y, width: 1, height: 1)
    }
  }

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 WKWebView is presented with their new modal presentation style, the UIDocumentMenuViewController is presented with UIModalPresentationPopover style, even on phones, which was until iOS 13 simply not the case.

I updated my code so now it sets the .sourceView/.sourceFrame/.barButtonItem only for phone types, because tablet types will be handled by iOS it self correctly.


I also encountered a similar crash.

You may fix it by setting modalPresentationStyle to .fullScreen.