Skip to content

Commit

Permalink
feat: extract visibleViewController in UIApplication extension and us…
Browse files Browse the repository at this point in the history
…e it
  • Loading branch information
AlexT84 committed Aug 2, 2023
1 parent 82c96a9 commit 3f91faf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
23 changes: 1 addition & 22 deletions ios/Classes/InAppBrowser/InAppBrowserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions ios/Classes/UIApplication+VisibleViewController.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 3f91faf

Please sign in to comment.