ioswkwebviewuipopovercontrollerios13

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


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


Solution

  • 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.