iOS
UIViewController
AppDelegate
Objective-C
iOSDevelopment

Get the current displaying UIViewController on the screen in AppDelegate.m

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Understanding how to get the current displaying `UIViewController` on the screen in `AppDelegate.m` is essential for developers who want to implement certain functionality globally in their iOS applications. Accessing the current view controller can be particularly useful for debugging, analytics, or when you need to send data between objects that don't have direct access to each other.

Technical Explanation

In iOS development, the `UIViewController` class serves as the foundation for handling the user interface for a part of your app. Every screen or interface typically has its own view controller. However, fetching the currently displayed view controller from the `AppDelegate` involves traversing the view controller hierarchy, as the `AppDelegate` is primarily designed for handling application-level events rather than view-specific interactions.

In `AppDelegate.m`, you'll find methods like `application:didFinishLaunchingWithOptions:` that are automatically executed during the app lifecycle. Here's a method you might implement within the `AppDelegate` to retrieve the active `UIViewController`.

Retrieving the Current View Controller

To get the currently displayed `UIViewController`, you may need to consider the following approach:

  1. Get the Root View Controller: Start by accessing the root view controller, which is typically set in the `AppDelegate`.
  2. Traverse the View Controller Hierarchy: If the root view controller is a container like `UINavigationController` or `UITabBarController`, you need to navigate through these containers to find the currently presented view controller.

Here's a step-by-step code example:

• (UIViewController *)getCurrentViewController { • (UIViewController *)getVisibleViewControllerFrom:(UIViewController *)rootViewController {

Root View Controller: The root view controller is fetched from the key window. This is the starting point of our traversal. • UINavigationController: If the root view controller is a navigation controller, the currently visible view controller is its visible view controller. • UITabBarController: If it's a tab bar controller, the currently selected view controller is accessed. • Presented View Controller: If the view controller has a presented view controller, it continues down that path, recursively checking for nested presentations. • Base Case: Returns the current view controller when there are no more presentations to follow. • Global Logging: Log view appearances globally for analytics purposes. This gives insight into which screens users interact with the most. • Global Event Handling: Handle universal events like internet connectivity issues or push notifications that may need user interaction, ensuring the event is directed to the active view controller. • Debugging: Determine which view controllers are active at any point in time during testing or after receiving error reports.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.