How can I add sceneDelegate to an existing project?

Asked on 03/24/2025

1 search

To add a sceneDelegate to an existing project, you would typically follow these steps:

  1. Open your project in Xcode: Make sure you have your project open in Xcode.

  2. Add a SceneDelegate file: If your project doesn't already have a SceneDelegate.swift file, you can add one by creating a new Swift file and naming it SceneDelegate.swift.

  3. Implement the SceneDelegate class: In the SceneDelegate.swift file, implement the SceneDelegate class. This class should conform to the UIWindowSceneDelegate protocol. Here is a basic template:

    import UIKit
    
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let windowScene = (scene as? UIWindowScene) else { return }
            window = UIWindow(windowScene: windowScene)
            window?.rootViewController = YourRootViewController() // Replace with your root view controller
            window?.makeKeyAndVisible()
        }
    
        // Implement other UIWindowSceneDelegate methods as needed
    }
    
  4. Update Info.plist: Ensure that your Info.plist file is configured to use the SceneDelegate. You should have an entry for Application Scene Manifest with a sub-entry for Scene Configuration. Under Scene Configuration, there should be a Default Configuration with a Delegate Class Name set to $(PRODUCT_MODULE_NAME).SceneDelegate.

  5. Remove AppDelegate window code: If your AppDelegate is setting up the window, you should remove that code since the SceneDelegate will now handle it.

This process is generally applicable to projects targeting iOS 13 and later, where the scene-based lifecycle is used. If you need more detailed guidance, you might want to refer to the session on Evolve your document launch experience (03:13) which discusses getting started with UIKit, including scene management.