title: How to check if the top most view controller is an ImagePickerController
body: <p>I use the <a href="https://stackoverflow.com/a/39416067/4833705">following</a> to check for the top most view controller. I need to check if the top view controller is an ImagePickerController</p>
  <pre><code>guard let window = UIApplication.shared.windows.first(where: \.isKeyWindow) else { return }
          
  guard let topVC = window.topViewController() else { return }
  
  if topVC.isKind(of: ImagePickerController.self) {
      // ...
  }
  </code></pre>
  <p>but I get an error <a href="https://i.stack.imgur.com/VbyVq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VbyVq.png" alt="enter image description here" /></a></p>
  <p>How can I check if the top vc has/is an imagePicker presented?</p>
  <pre><code>extension UIWindow {
      func topViewController() -&gt; UIViewController? {
          var top = self.rootViewController
          while true {
              if let presented = top?.presentedViewController {
                  top = presented
              } else if let nav = top as? UINavigationController {
                  top = nav.visibleViewController
              } else if let tab = top as? UITabBarController {
                  top = tab.selectedViewController
              } else {
                  break
              }
          }
          return top
      }
  }
  </code></pre>