11

I have a SwiftUI/SpriteKit project. In an error-laden quest to change the bundle identifier, I decided to create a new project and copy my files over.

Now I've got this new project with all my old files but when I run it, I get a blank screen because my SceneDelegate function scene(_:willConnectTo:options:) is not being called.

According to the documentation, a SceneDelegate setup will work if you make the appropriate changes to your info.plist. However, I've made those changes, and my SceneDelegate remains inoperative.

In my info.plist, I have the following: enter image description here

Here's the relevant part of my SceneDelegate:

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    
    static var mainData = MainData()

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView.environmentObject(SceneDelegate.mainData))
            self.window = window
            window.makeKeyAndVisible()
        }
    }

    //...more stuff here
}

Question: Why isn't my SceneDelegate working? What am I doing wrong?

Thank you!

4
  • what you get in debugger window while launching app?
    – user9137841
    Commented May 3, 2021 at 13:01
  • Are you migrating from AppDelegate or the new App lifecycle?
    – aheze
    Commented May 3, 2021 at 14:34
  • @RB's I get nothing in the logs, actually.
    – West1
    Commented May 3, 2021 at 14:43
  • @aheze Migrating from AppDelegate
    – West1
    Commented May 3, 2021 at 14:44

1 Answer 1

47

I have no idea why my SceneDelegate wasn't working, but I did two things:

  1. Deleted the app from the simulator I was using.
  2. Restarted Xcode.

My SceneDelegate now works as expected.

3
  • 1
    wow - restarting XCode did the trick ... sigh
    – TheEye
    Commented Dec 22, 2022 at 13:16
  • 1
    Thank you for this, I was going crazy why it's not working. Xcode is being Xcode again
    – Istvan
    Commented Jan 1, 2023 at 7:18
  • 4
    just delete & reinstall works for me, thanks bro
    – eczn
    Commented Mar 13, 2023 at 11:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.