Why is the AppDelegate.swift window optional?

I read Apple docs when I found this sentence:

The AppDelegate class contains one property: window .

var window: UIWindow?

This repository contains a link to the application window. This window is the root of the hierarchy of representations of your applications. Here, all the contents of your application are drawn. Note that the window property is optional , which means that it may not be (be nil ) at some point.

I do not understand why this property may be null at some point? What's the matter for him (come) nil?

+6
source share
3 answers

You may not always need this. For example, when calling these two methods:

 application(_:performFetchWithCompletionHandler:) application(_:handleEventsForBackgroundURLSession:completionHandler:) 

your application will not be shown to the user, so there is no need for a window .

As always, more at docs

Now I'm not sure if this is an inherent reason, but it seems like a good enough opportunity (at least for me). Although, if someone can provide additional information, I will be happy to learn something else.

+4
source

This becomes more apparent when you create a window programmatically, instead of using the main storyboard, which automatically sets the window property.

You may not want or cannot create a window immediately after creating the delegate object ( AppDelegate in your case). Usually you do not need to create a window and set the property until application(_:didFinishLaunchingWithOptions:) called. Thus, until the window is created and the property is set, it will be nil .
Like Losiowaty has already been said , this also happens when the application starts, but is not displayed to the user. when they only process location updates or other information in the background.

If the property were not optional, you would have to create a window at the time you created the AppDelegate object, which is neither desirable nor necessary.

+4
source

When you close the application, your application can still receive silentNotifications or download data in the background .

In the pictures below, red Y85jZ.png surrounded when your application is still doing something, but it is no longer displayed on the screen. It is in the background, so AppDelegate no longer needs a window . The result will be set to nil

Simple review

enter image description here


Detailed review

enter image description here

FWIW, the code below will not make the application launch using vc .

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let vc = ViewController() window?.rootViewController = vc window?.makeKeyAndVisible() return true } 

Why is this not working? Since the window property is optional, it is initially set to nil. It must be created

The code below will work

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let vc = ViewController() window = UIWindow(frame: UIScreen.main.bounds) // Now it is instantiated!! window?.rootViewController = vc window?.makeKeyAndVisible() return true } 
+3
source

All Articles