feat: extract visibleViewController in UIApplication extension and use it

This commit is contained in:
alext84 2023-08-02 13:34:58 +03:00
parent 82c96a957f
commit 3f91faf859
3 changed files with 33 additions and 23 deletions

View File

@ -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
}

View File

@ -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()

View File

@ -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
}
}