Opening view controller from app delegate using swift
Codemia Knowledge Hub ·
When developing an iOS application in Swift, there may be scenarios where you need to launch a specific view controller directly from the `AppDelegate`. This is common when managing variations in the app's initial state upon launch, such as opening a specific view based on user authentication status or a push notification.
The Role of `AppDelegate`
The `AppDelegate` class in an iOS application is responsible for handling application lifecycle events. One of the key methods in this class is `application(_:didFinishLaunchingWithOptions:)`, which is called when the app has completed its launch process. This makes it an ideal place to configure your app’s initial view controller.
Accessing the `UIWindow`
The iOS app's `UIWindow` object acts as a backdrop for your app's user interface. To set the root view controller programmatically from the `AppDelegate`, you will first need to access this window or create one if it doesn't exist.
Setting the Initial View Controller
To navigate to a specific view controller from the `AppDelegate`, follow these steps:
- Access the Window: Within your AppDelegate, access the window property. If it's not set, you will need to instantiate it.
- Instantiate the View Controller: Create an instance of the view controller you want to display.
- Set the Root View Controller: Assign the instantiated view controller to the window's `rootViewController` property.
- Make the Window Visible: Ensure the window is visible by invoking `makeKeyAndVisible()`.
Below is a simple example:
- Storyboard Integration: If you use storyboards, you might want to instantiate view controllers using the storyboard identifiers. This can be achieved using methods like `instantiateViewController(withIdentifier:)`.
- SceneDelegate: For projects using the scene delegation system (iOS 13 and later), the concept extends similarly to `SceneDelegate`, where the initial setup is managed in the `scene(_:willConnectTo:options:)` method. You would similarly create and assign the root view controller there.
- Conditional Routing: You can include logic in `didFinishLaunchingWithOptions` to determine which view controller to present. For example, use user defaults or a session manager to check authentication status.
- Performance Considerations: Ensure that view controllers instantiated in `AppDelegate` are not doing heavy operations synchronously, as this might delay the app launch experience.
- Design Patterns: Utilize design patterns like MVVM or Coordinator for more complex view controller routing, enabling a cleaner separation of concerns.
Related reading
- openURL deprecated in iOS 10
- Operation Queue vs Dispatch Queue for iOS Application
- operator in Swift
- Opt out of UISceneDelegate/SwiftUI on iOS
- Optional array vs. empty array in Swift
- optional closure and check if it is nil
- Outlets cannot be connected to repeating content iOS
- Override back button to act like home button
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.