2 // UIApplication+OAuthSwift.swift
5 // Created by phimage on 11/12/15.
6 // Copyright © 2015 Dongri Jin. All rights reserved.
9 #if os(iOS) || os(tvOS)
12 extension UIApplication {
13 static var topViewController: UIViewController? {
14 #if !OAUTH_APP_EXTENSIONS
15 return UIApplication.shared.topViewController
21 var topViewController: UIViewController? {
22 guard let rootController = self.keyWindow?.rootViewController else {
25 return UIViewController.topViewController(rootController)
29 extension UIViewController {
31 static func topViewController(_ viewController: UIViewController) -> UIViewController {
32 guard let presentedViewController = viewController.presentedViewController else {
35 #if !topVCCastDisabled
36 if let navigationController = presentedViewController as? UINavigationController {
37 if let visibleViewController = navigationController.visibleViewController {
38 return topViewController(visibleViewController)
40 } else if let tabBarController = presentedViewController as? UITabBarController {
41 if let selectedViewController = tabBarController.selectedViewController {
42 return topViewController(selectedViewController)
46 return topViewController(presentedViewController)