SceneDelegate 函数从未被调用

Wes*_*st1 10 lifecycle info.plist swift swiftui uiscenedelegate

我有一个 SwiftUI/SpriteKit 项目。在一次充满错误的更改包标识符的任务中,我决定创建一个项目并复制我的文件。

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

根据文档SceneDelegate如果您对info.plist. 然而,我已经做了这些改变,但我的SceneDelegate仍然不起作用。

在我的 中info.plist,我有以下内容: 在此输入图像描述

这是我的相关部分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
}
Run Code Online (Sandbox Code Playgroud)

问: 为什么我不SceneDelegate工作?我究竟做错了什么?

谢谢你!

Wes*_*st1 40

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

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

SceneDelegate现在按预期工作。

  • 只需删除并重新安装对我有用,谢谢兄弟 (4认同)