SceneDelegate 函数永远不会被调用

问题描述

我有一个 SwiftUI/SpriteKit 项目。为了更改包标识符,我决定创建一个项目并复制我的文件

现在我有了这个包含所有旧文件的新项目,但是当我运行它时,我得到一个空白屏幕,因为我的 SceneDelegate 函数 scene(_:willConnectTo:options:) 没有被调用

根据 documentation,如果您对 SceneDelegate 进行适当的更改,info.plist 设置将起作用。但是,我已经进行了这些更改,而我的 SceneDelegate 仍然无效。

在我的 info.plist 中,我有以下内容

enter image description here

这是我的 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
}

问题: 为什么我的 SceneDelegate 不工作?我做错了什么?

谢谢!

解决方法

我不知道为什么我的 SceneDelegate 不起作用,但我做了两件事:

  1. 从我使用的模拟器中删除了该应用。
  2. 重新启动 Xcode。

我的 SceneDelegate 现在按预期工作。