From 3f91faf8596c892faff9c296d023dbcef3304bad Mon Sep 17 00:00:00 2001 From: alext84 Date: Wed, 2 Aug 2023 13:34:58 +0300 Subject: [PATCH] feat: extract visibleViewController in UIApplication extension and use it --- .../InAppBrowser/InAppBrowserManager.swift | 23 +------------- .../ChromeSafariBrowserManager.swift | 2 +- .../UIApplication+VisibleViewController.swift | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 ios/Classes/UIApplication+VisibleViewController.swift diff --git a/ios/Classes/InAppBrowser/InAppBrowserManager.swift b/ios/Classes/InAppBrowser/InAppBrowserManager.swift index e0ea5989..c6e06bb2 100755 --- a/ios/Classes/InAppBrowser/InAppBrowserManager.swift +++ b/ios/Classes/InAppBrowser/InAppBrowserManager.swift @@ -98,27 +98,6 @@ public class InAppBrowserManager: ChannelDelegate { presentViewController(webViewController: webViewController) } - - private func visibleViewController() -> UIViewController? { - guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController else { - return nil - } - return getVisibleViewController(rootViewController) - } - - private func getVisibleViewController(_ rootViewController: UIViewController) -> UIViewController? { - if let presentedViewController = rootViewController.presentedViewController { - return getVisibleViewController(presentedViewController) - } - if let navigationController = rootViewController as? UINavigationController { - return navigationController.visibleViewController - } - if let tabBarController = rootViewController as? UITabBarController { - return tabBarController.selectedViewController - } - return rootViewController - } - public func presentViewController(webViewController: InAppBrowserWebViewController) { let storyboard = UIStoryboard(name: InAppBrowserManager.WEBVIEW_STORYBOARD, bundle: Bundle(for: InAppWebViewFlutterPlugin.self)) let navController = storyboard.instantiateViewController(withIdentifier: InAppBrowserManager.NAV_STORYBOARD_CONTROLLER_ID) as! InAppBrowserNavigationController @@ -131,7 +110,7 @@ public class InAppBrowserManager: ChannelDelegate { animated = false } - guard let visibleViewController = visibleViewController() else { + guard let visibleViewController = UIApplication.shared.visibleViewController else { assertionFailure("Failure inet the visibleViewController!") return } diff --git a/ios/Classes/SafariViewController/ChromeSafariBrowserManager.swift b/ios/Classes/SafariViewController/ChromeSafariBrowserManager.swift index 3e4a9bc6..5887ea77 100755 --- a/ios/Classes/SafariViewController/ChromeSafariBrowserManager.swift +++ b/ios/Classes/SafariViewController/ChromeSafariBrowserManager.swift @@ -90,7 +90,7 @@ public class ChromeSafariBrowserManager: ChannelDelegate { if #available(iOS 9.0, *), let plugin = plugin { - if let flutterViewController = UIApplication.shared.delegate?.window.unsafelyUnwrapped?.rootViewController { + if let flutterViewController = UIApplication.shared.visibleViewController { // flutterViewController could be casted to FlutterViewController if needed let safariSettings = SafariBrowserSettings() diff --git a/ios/Classes/UIApplication+VisibleViewController.swift b/ios/Classes/UIApplication+VisibleViewController.swift new file mode 100644 index 00000000..fdab0031 --- /dev/null +++ b/ios/Classes/UIApplication+VisibleViewController.swift @@ -0,0 +1,31 @@ +// +// UIApplication+VisibleViewController.swift +// flutter_inappwebview +// +// Created by Alexandru Terente on 02.08.2023. +// + +import UIKit + +extension UIApplication { + + var visibleViewController: UIViewController? { + guard let rootViewController = keyWindow?.rootViewController else { + return nil + } + return getVisibleViewController(rootViewController) + } + + private func getVisibleViewController(_ rootViewController: UIViewController) -> UIViewController? { + if let presentedViewController = rootViewController.presentedViewController { + return getVisibleViewController(presentedViewController) + } + if let navigationController = rootViewController as? UINavigationController { + return navigationController.visibleViewController + } + if let tabBarController = rootViewController as? UITabBarController { + return tabBarController.selectedViewController + } + return rootViewController + } +}