Skip to content

Instantly share code, notes, and snippets.

@sebj
Last active May 13, 2024 15:10
Show Gist options
  • Save sebj/bae35d853be780434a894b62e21faaff to your computer and use it in GitHub Desktop.
Save sebj/bae35d853be780434a894b62e21faaff to your computer and use it in GitHub Desktop.
Add a scene delegate back to a SwiftUI Project

Define an application delegate

See also: UIApplicationDelegate documentation

// AppDelegate.swift
@UIApplicationMain
final class AppDelegate: NSObject, UIApplicationDelegate {

    func application(
        _ application: UIApplication,
        configurationForConnecting connectingSceneSession: UISceneSession,
        options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
        let sceneConfiguration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
        sceneConfiguration.delegateClass = SceneDelegate.self
        return sceneConfiguration
    }
}

Define a scene delegate

See also: UIWindowSceneDelegate documentation

// SceneDelegate.swift
final class SceneDelegate: NSObject, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(
        _ scene: UIScene,
        willConnectTo session: UISceneSession,
        options connectionOptions: UIScene.ConnectionOptions
    ) {
        guard let windowScene = scene as? UIWindowScene else {
            return
        }

        let window = UIWindow(windowScene: windowScene)

        // TODO: Do something with `window`
        let contentView = ContentView() // or whatever your root SwiftUI view is called
        window.rootViewController = UIHostingController(rootView: contentView)
        window.makeKeyAndVisible()

        self.window = window
    }
}

Specify the scene delegate as part of the app's Info.plist file

In a newly created SwiftUI project:

  1. Click your project name in the left file navigator
  2. Click the app target in the following screen
  3. Click 'Info'
  4. Expand 'Application Scene Manifest'
  5. Add a row inside 'Application Scene Manifest' – Xcode will generate a <Your-Target-Name>-Info.plist file in the left file navigator
  6. Right click on the generated plist file, and choose Open As → Source Code
  7. Add the scene configuration below inside the <dict> for UIApplicationSceneManifest

In a project with an existing Info.plist file:

  1. Right click on the plist file in the left file navigator, and choose Open As → Source Code
  2. Add the scene configuration below inside the <dict> for UIApplicationSceneManifest
<key>UISceneConfigurations</key>
<dict>
    <key>UIWindowSceneSessionRoleApplication</key>
    <array>
        <dict>
            <key>UISceneConfigurationName</key>
            <string>Default Configuration</string>
            <key>UISceneDelegateClassName</key>
            <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
        </dict>
    </array>
</dict>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment