1

I use the following to check for the top most view controller. I need to check if the top view controller is an ImagePickerController

guard let window = UIApplication.shared.windows.first(where: \.isKeyWindow) else { return }
        
guard let topVC = window.topViewController() else { return }

if topVC.isKind(of: ImagePickerController.self) {
    // ...
}

but I get an error enter image description here

How can I check if the top vc has/is an imagePicker presented?

extension UIWindow {
    func topViewController() -> 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
    }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • 1
    ```if let imagePicker = UIApplication.shared.windows.first?.topViewController() as? UIImagePickerController { }``` check this. Or ```UIImagePickerController.self```? – Raja Kishan Jun 11 '21 at 15:59

1 Answers1

2

You are setting ImagePickerController.self but the class name is UIImagePickerController

You can use like this

if let imagePicker = UIApplication.shared.windows.first?.topViewController() as? UIImagePickerController {
 // Do your stuf
}

Or

if topVC.isKind(of: UIImagePickerController.self) {
    // ...
}

Note: By using this you can not cast the top view controller as a UIImagePickerController. As it's designed by apple.

You can use this and access the view controller by this.

if let pickerHostClass = NSClassFromString("PUPhotoPickerHostViewController"), topVC.isKind(of: pickerHostClass) {
   topVC.view.alpha = 0.5
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • I can accept in 6 minutes. You are correct, I forgot to add the UI in front of ImagePickerController. The first way you did it I didn't think of that, I always use the second way. Thanks for that too :) – Lance Samaria Jun 11 '21 at 16:06
  • have you checked this condition is satisfied after running this? it solves your compile error but I don't think you can use it like this. – Raja Kishan Jun 11 '21 at 16:09
  • It definitely solved the compile error and that is why I accepted it, but I haven't checked yet. Why would it not work? I works for other scenarios, for example I have a class named MyClass. If I use `if topVC.isKind(of: MyCLass.self { print("MyClass is topVC") }` that works fine. Why wouldn't it work for the imagePicker? – Lance Samaria Jun 11 '21 at 16:12
  • It because ```MyCLass.self``` is created by you and ```UIImagePickerController``` is designed by apple you can not do more with this so. – Raja Kishan Jun 11 '21 at 16:13
  • Gimme a couple of minutes, I'm going to check it now. I'll let you know what happens – Lance Samaria Jun 11 '21 at 16:14
  • oh shoot, you're right, the print statement doesn't print. Here's the situation. I have a remote notification that comes in, if the user presses it, it takes them to the NotificationsController from whatever vc they are currently on, and that works fine. But i said to myself what happens if the user is using the imagePicker, in that case if it's the topVC I'll just `return` meaning the NotificationsVC won't get pushed on. I'm not doing anything with the imagePicker other than checking to see if it visible. I tried both of your ways, and you're right, neither worked for my use case. – Lance Samaria Jun 11 '21 at 16:25
  • 1
    That worked :) THANKS!!!. You taught me something new :) – Lance Samaria Jun 11 '21 at 16:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233670/discussion-between-lance-samaria-and-raja-kishan). – Lance Samaria Jun 11 '21 at 16:30