Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25763

Access UINavigationController From AppDelegate

I need to access UINavigationController in AppDelegate

Here is what I tried

func applicationDidBecomeActive(_ application: UIApplication) {
    let syncManager = SyncManager()
    let navigationController: UINavigationController = UIStoryboard(
        name: "Main",
        bundle: nil
    ).instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController
    syncManager.setNavigationController(navigationController)
    syncManager.sync()
}

This doesn't seem to work.

How do I access UINavigationController from the AppDelegate?

Thanks.

Update

I am using this library to display the notification in the application when the app is active. this library requires an instance of navigationController to display the notification.

When I use this code in AppDelegate the notification is not displayed, but when I use in the in ViewController the notifications are displayed.

Upvotes: 0

Views: 1654

Answers (2)

Tharzeez
Tharzeez

Reputation: 1343

You can try this

let navigationController = application.windows.first?.rootViewController as? UINavigationController

it wont crash even if your rootViewController is TabBarController . it will safely unwrap it .

Upvotes: 1

Punit
Punit

Reputation: 1340

Try this:

let navigationController = application.windows[0].rootViewController as! UINavigationController

Upvotes: 4

Related Questions