How to connect SceneDelegate without using `SceneConfiguration` in info.plist in iOS?

Viewed 1040

I'm new to the SceneDelegate setup. I notice that in info.plist, we have a mapping under Scene Configuration:

Configuration Name: Default Configuration <- this key
Delegate Class Name: $(PRODUCT_MODULE_NAME).SceneDelegate <- this file

Then in AppDelegate's connectingSceneSession, we return the config with the key Default Configuration.

return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)

Is it possible to avoid this (key -> file) mapping in info.plist, and hook it up programmatically?

Note: I am not asking about storyboard. There are tons of tutorials about removing storyboard and I know that.

1 Answers

Remove "Scene Configuration" (UISceneConfigurations) entry from Info.plist, and instantiate UISceneConfiguration in AppDelegate:

// class AppDelegate
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    if connectingSceneSession.role == UISceneSession.Role.windowApplication {
        let config = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
        config.delegateClass = SceneDelegate.self
        config.storyboard = UIStoryboard(name: "Main", bundle: nil)
        return config
    }
    fatalError("Unhandled scene role \(connectingSceneSession.role)")
}

From the manual:

Implement this method if you do not include scene-configuration data in your app's Info.plist file, or if you want to alter the scene configuration data dynamically. UIKit calls this method shortly before creating a new scene.

Related