14

Before we all get used to the approach when in AppDelegate we create UIWindow and then set rootViewController for this Window. Sometimes when we needed to have a custom alert we can create a new UIWindow that will be above.

Now Xcode automatically creates UIScene and creates UIWindow base on this scene. I would appreciate if somebody can explain in details how to use this scenes in real life and what is their major benefit from using just UIWindow in AppDelegate

Because this article doesn't explain much

Paul T.
  • 4,226
  • 6
  • 39
  • 84
  • 1
    Your _article_ word is linked to the `UIScene` documentation, but I'd recommend to read [Scenes](https://developer.apple.com/documentation/uikit/app_and_environment/scenes), [Supporting Multiple Windows](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/supporting_multiple_windows_on_ipad), etc. What is major benefit? Multiple UI instances of your app = multiple windows for example. – zrzka May 12 '20 at 14:24

2 Answers2

5

The documentation

You should read the App and Environment Article from Apple instead of UIScene documentation.


Explaination

As it says about the scenes:

Scene, Manage multiple instances of your app’s UI simultaneously, and direct resources to the appropriate instance of your UI.

We had only one scene back then before iOS 13, So the only thing we need to run ViewControllers simultaneously was multiple Windows on top of each other. But now, each application can have multiple instances running at the same time! Each scene has its own state and it might be in the foreground while others are in the background or are suspended, while Window was completely depended on the application itself.

Imagine we have 2 view controllers (consider there are no scenes) running on the left and right side of the device and then we need to show a banner. Using the old window method will show the banner on both of them! And if you need to pick one, you may end up finding the correct controller and presenting the banner on it, (I think all of us done this method before getting familiar with UIWindow)

So apple introduced Scene, a container for each separate instance of the app. So you can manage each one separately and each of them acts like a separate app. It has its own windows and controllers. But all of them are managed by a single object, UIApplication.shared and it has a delegate to handle general events (usually from outside of the app) and entire application life cycle.


Community
  • 1
  • 1
Mojtaba Hosseini
  • 47,708
  • 12
  • 157
  • 176
4

The biggest thing is that scenes set up multiple window support (currently only available on iPadOS and macOS). It is also useful for SwiftUI setup on iOS.

The SceneDelegate controls what is displayed on the screen "to manage life-cycle events in one instance of your app's user interface.", while the AppDelegate controls your entire app life-cycle.

You'll also notice keyWindow no longer exists for iOS. It's quite possible multiple window support is coming to iOS, but this is speculation right now.

Hopefully this clarifies things a bit and gives you an idea of why Apple is updating the implementation.

This article gives some more data regarding SceneDelegate that you may find useful: https://www.donnywals.com/understanding-the-ios-13-scene-delegate/

elliott-io
  • 1,264
  • 3
  • 8
  • thank you +1, a good article, especially interesting about NSUserActivity restoration. But I started a bounty, so I hope the answer will contain more information with examples and a bit more description (because not everybody want to read a long article). So if nobody gives this good answer, I will give bounty to you – Paul T. May 14 '20 at 13:32