Skip to content

Instantly share code, notes, and snippets.

@bergusman
Created July 22, 2021 10:57
Show Gist options
  • Save bergusman/d41a3355b61e3c4b579d888503b84afe to your computer and use it in GitHub Desktop.
Save bergusman/d41a3355b61e3c4b579d888503b84afe to your computer and use it in GitHub Desktop.
iOS Application UIViewController's hierarchy monitor
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private func setupWindow() {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
}
// MARK: - User Interface
private func showUserInterface() {
}
// MARK: - User Interface Monitoring
private var userInterfaceMonitoringTimer: Timer?
private func startUserInterfaceMonitoring() {
lastMonitoringResult = ""
userInterfaceMonitoringTimer?.invalidate()
userInterfaceMonitoringTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
self.monitorUserInterface()
}
monitorUserInterface()
}
private func stopUserInterfaceMonitoring() {
userInterfaceMonitoringTimer?.invalidate()
userInterfaceMonitoringTimer = nil
}
private var lastMonitoringResult = ""
private func monitorUserInterface() {
guard let rootVC = window?.rootViewController else {
return
}
var result = ""
var presentedVCs = Set<UIViewController>()
func build(_ vc: UIViewController, indent: String = "") {
result += indent + "\(vc)\n"
if let presentedVC = vc.presentedViewController {
if presentedVCs.contains(presentedVC) {
result += indent + " presented: \(presentedVC)\n"
} else {
presentedVCs.insert(presentedVC)
result += indent + " presented:\n"
build(presentedVC, indent: indent + " ")
}
}
if let presentingVC = vc.presentingViewController {
result += indent + " presenting: \(presentingVC)\n"
}
if vc.children.count > 0 {
result += indent + " children:\n"
for child in vc.children {
build(child, indent: indent + " ")
}
}
}
build(rootVC)
if lastMonitoringResult != result {
lastMonitoringResult = result
print(result)
}
}
// MARK: - UIApplicationDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setupWindow()
showUserInterface()
startUserInterfaceMonitoring()
return true
}
}
<RootViewController: 0x10b306c20>
presented:
<ProfileViewController: 0x10930a130>
presented:
<UINavigationController: 0x10b83c000>
presenting: <ProfileViewController: 0x10930a130>
children:
<PersonalDataViewController: 0x10b308ad0>
presenting: <ProfileViewController: 0x10930a130>
<PersonalEmailViewController: 0x106f16df0>
presenting: <ProfileViewController: 0x10930a130>
presenting: <RootViewController: 0x10b306c20>
children:
<UINavigationController: 0x10c81ee00>
presented: <ProfileViewController: 0x10930a130>
children:
<HomeViewController: 0x109216270>
presented: <ProfileViewController: 0x10930a130>
<MenuViewController: 0x106b2bf70>
presented: <ProfileViewController: 0x10930a130>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment