SceneDelegate function is never called

4.7k views Asked by At

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!

1

There are 1 answers

3
West1 On BEST ANSWER

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.