Question: I want to know which view is present when the app is going foreground. 

How is that possible?

```
func applicationWillEnterForeground(_ application: UIApplication) {

            if (storyboardID == "myview") {

               //do sth

            }

        }

```

Answer: 
```
func getCurrentViewController() -> UIViewController? {

      if let rootController = UIApplication.shared.keyWindow?.rootViewController {
      var currentController: UIViewController! = rootController
         while (currentController.presentedViewController != nil) {
                currentController = currentController.presentedViewController
            }
            return currentController
        }
        return nil
    }

```

Use the above code you will get the current View Controller.

Usage Example
-------------

```
func applicationWillEnterForeground(_ application: UIApplication) {
    if let storyboardID = getCurrentViewController()?.restorationIdentifier, 
           storyboardID == "myview") {
           //do sth
        }
    }

```
```
public extension UIWindow {
    public var visibleViewController: UIViewController? {
        return UIWindow.getVisibleViewControllerFrom(self.rootViewController)
    }

    public static func getVisibleViewControllerFrom(_ vc: UIViewController?) -> UIViewController? {
        if let nc = vc as? UINavigationController {
            return UIWindow.getVisibleViewControllerFrom(nc.visibleViewController)
        } else if let tc = vc as? UITabBarController {
            return UIWindow.getVisibleViewControllerFrom(tc.selectedViewController)
        } else {
            if let pvc = vc?.presentedViewController {
                return UIWindow.getVisibleViewControllerFrom(pvc)
            } else {
                return vc
            }
        }
    }
}

```

Simple extension for UIApplication in Swift (cares even about moreNavigationController within UITabBarController:

```
extension UIApplication {
    class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {

        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }

        if let tab = base as? UITabBarController {
            let moreNavigationController = tab.moreNavigationController

            if let top = moreNavigationController.topViewController where top.view.window != nil {
                return topViewController(top)
            } else if let selected = tab.selectedViewController {
                return topViewController(selected)
            }
        }

        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }

        return base
    }
}

```

Simple usage:

```
   if let rootViewController = UIApplication.topViewController() {
        //do sth with root view controller
    }

```