iosswiftuiwindowuiscenedelegate

Is SceneDelegate mandatory?


The question says it all.

There's the following line in apple documentation,

Scenes are opt in, but you must support them if you want to display multiple copies of your app’s UI simultaneously.

It's not mentioned whether the SceneDelegate is mandatory or optional or a recommendation (I'm aware that the SceneDelgate is used to handle the lifecycle of the scene). In a iOS 13+ device, can I create an app without SceneDelegate?

I defined the UIWindow variable in the AppDelgate and set the ViewController in application(didFinishLaunchingWithOptions), like before iOS 13. But that doesn't work and displays a blank screen.

And thus, this question. Is Scenedelgate mandatory even if we don't opt for multiple scenes? Here's my observation:

Multiple scenes are optional. But you still have to support at least one scene... and for that, SceneDelegate is mandatory.

If I'm wrong and SceneDelegate is optional (like described in this stackoverflow post), please help with the flows.


Solution

  • In a iOS 13+ device, can I create an app without SceneDelegate?

    Yes. All apps do have scenes, but if you go with the app-delegate-only architecture, a single UIWindowScene will be created behind the scenes (pun intended). The app will run perfectly well on both iPhone and iPad, with all the usual lifetime events arriving as usual into the app delegate (though I do not know what will happen on Apple Silicon).

    I defined the UIWindow variable in the AppDelgate and set the ViewController in application(didFinishLaunchingWithOptions), like before iOS 13. But that doesn't work and displays a blank screen.

    This is because you did not correctly convert from the scene delegate architecture, which is provided by the app template you started with, to an app delegate architecture. Basically, you've left pieces of the scene delegate architecture lying around. Perhaps you did not delete all references to the scene delegate; perhaps you forgot to delete the UIApplicationSceneManifest entry from the Info.plist. Whatever the reason, the result is that the runtime thinks this app still uses scene delegate architecture, and it then fails to find certain pieces that it's looking for and shows the blank screen.

    So, just to sum up what to do when you've started with the built-in app template:

    1. Delete the SceneDelegate.swift file.

    2. In the App Delegate, delete the two UISceneSession methods.

    3. In the App Delegate, add a window reference as an instance property: var window: UIWindow?

    4. In the Info.plist, completely delete the entire UIApplicationSceneManifest entry (and save).

    5. Profit.