'View is not in the Window Hierarchy Error

I am running into an error where my view controller is dismissed upon attempting to upload a file.

Basically, the issue is that I have a WKWebView, that loads a website. Within this website, there is an "Upload File" button which needs a picture. The screen then proceeds to show the option of using Camera / Photo Library.

I tried adding all the corresponding pList Privacy settings, but when the "Upload File" button is clicked the View-Controller (Shown via Push) dismisses back to the home screen and gives me the following error:

Warning: Attempt to present UIImagePickerController on UINavigationController whose view is not in the window hierarchy!

This is my code

import UIKit import WebKit

class BizOwnerVC: UIViewController, WKUIDelegate {

var NewWebView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    NewWebView = WKWebView(frame: .zero, configuration: webConfiguration)
    NewWebView.uiDelegate = self
    view = NewWebView
}

override func viewDidLoad() {
    super.viewDidLoad()

    let myURL = URL(string: "https://cdlcheck.knack.com/dl-check#logged-in/")
    let myRequest = URLRequest(url: myURL!)
    NewWebView.load(myRequest)
}


Solution 1:[1]

My solution was to dismiss the ViewController containing the WKWebView before I opened the other window. Hope this helps somebody

Solution 2:[2]

Add the following code to the View Controller which contains the Web View :

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) 
{
    if self.presentedViewController != nil {
        super.dismissViewControllerAnimated(flag, completion: completion)
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Werner Kratochwil
Solution 2 Mukul More