App Delegate - UIWindow is not working properly

Hello everyone,


I am trying to edit a tab bar controller to display more items. I am using a UIWindow but it just does not work. I have followed some tutorials and one thing I noticed is that in everyone of the the window property is already there.


In my version of Xcode it is not. I have to declare it myself.


Can you help me? Here is the code I am using.


It always prints "exits with error". 😟


class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    //Window em que a aplicação está a ser executada.

    //Função chamada pelo iOS quando a app termina de carregar.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        
        if let tabBarController = window?.rootViewController as? UITabBarController {
            //Neste projeto, se repararmos no storyboard o primeiro ecrã que vemos faz referência a um tab bar controller, por isso está tudo dentro dele.
            
            //Por isso é que o default view controller é um tab bar controller.
            
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let viewController = storyboard.instantiateViewController(identifier: "NavController")
            
            //Adicionamos um tabBarItem ao view controller.
            viewController.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
            
            //The default value of this property is nil. When configuring a tab bar controller, you can use this property to specify the content for each tab of the tab bar interface. 
            tabBarController.viewControllers?.append(viewController)
            
        }
        else {
            print("Exits with error.")
        }
        
        return true
    }
Answered by Claude31 in 407476022

Probably window is nil here ; test it:


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 
         
        print(window)
        if let tabBarController = window?.rootViewController as? UITabBarController {

Do you have a scene delegaye in your app ?


In this case, it is normal, window is nil in AppDelegate.

Need to implement your code in

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {


Take care, only available for iOS 13 and over.

Below iOS 13, still need to do in AppDelegate.

Accepted Answer

Probably window is nil here ; test it:


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 
         
        print(window)
        if let tabBarController = window?.rootViewController as? UITabBarController {

Do you have a scene delegaye in your app ?


In this case, it is normal, window is nil in AppDelegate.

Need to implement your code in

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {


Take care, only available for iOS 13 and over.

Below iOS 13, still need to do in AppDelegate.

Thank you!!! 😀


I literally pasted the code in Scene Delegate and it worked perfectly.


Why do we have to use scene delegate with iOS 13? What is now done in App Delegate and how are app delegate and scene delegate related?

Scene are a new set up to allow multi scenes for the same app.

You could remove scene, but it is becoming mandatory on AppStore.


App Delegate is now essentially limited to launching.


To see what should be in each, just create an empty project and look at AppDelegate and SceneDelegate files.

You will see that window is no more in AppDelegate.


However, if you want to support iOS 12, you need to put again window property in AppDelegate and implement all the traditional func. And condition the sceneDelegate func to @available(iOS 13)


A bit fuzzy, but it is a transition period…

App Delegate - UIWindow is not working properly
 
 
Q