SceneDelegate function is never called
Asked Answered
G

1

12

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!

Giacomo answered 3/5, 2021 at 12:49 Comment(4)
what you get in debugger window while launching app?Settlings
Are you migrating from AppDelegate or the new App lifecycle?Blackfellow
@RB's I get nothing in the logs, actually.Giacomo
@Blackfellow Migrating from AppDelegateGiacomo
G
49

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.

Giacomo answered 3/5, 2021 at 16:19 Comment(3)
wow - restarting XCode did the trick ... sighGelinas
Thank you for this, I was going crazy why it's not working. Xcode is being Xcode againHellenhellene
just delete & reinstall works for me, thanks broRamin

© 2022 - 2024 — McMap. All rights reserved.