运行iOS应用程序时出现黑屏


12

我正在尝试在Xcode中创建一个新的iOS应用。我制作了一个主要的故事板,并在ViewController上添加了标签。当我运行应用程序时,首先显示标签,然后黑屏,没有任何错误。

我正在使用Xcode 11(Swift 5),并且此消息出现在输出中:

[SceneConfiguration] UIWindowSceneSessionRoleApplication的Info.plist配置“默认配置”包含UISceneDelegateClassName键,但无法加载名称为“ gina.SceneDelegate”的类

我不知道我的错误在哪里。

运行时黑屏



我认为您从项目中删除了SceneDelegate类
Alfi

下面的许多答案似乎都遗漏了错误所在,并描述了与所讨论的确切错误无关的修复程序。就我而言,我只是忘记将添加SceneDelegate到目标中,并选中固定它的框。
韦恩

Answers:


32

iOS 13

仅当目标是13或更大时。

SceneDelegateiOS 13之前不支持。如果您想使用SceneDelegate并且还想在iOS 13之前支持iOS,则必须对项目进行一些更改。

  1. 将可用性属性添加到SceneDelegate.swift文件中的整个类。
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
   ...
}
  1. AppDelegate.swift文件具有两个新SceneDelegate方法。也向他们添加可用性属性。
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  ...
}

@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  ...
}
  1. 最后,UIWindowAppDelegate.swift中添加对象。
class AppDelegate: UIResponder, UIApplicationDelegate {

    //Add this line
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    ...
}

iOS 12及更早版本

AppDelegate需要UIWindow财产。iOS版13使用SceneDelegate新的项目。指定UIWindow对象并删除SceneDelegate.swift文件。

如果SceneDelegate已从项目中删除了,则必须从Info.plist中删除Application Scene Manifest字典。

信息清单


太好了,谢谢
Mohammad Razipour

1

您需要像这样初始化窗口:

let window = UIWindow(windowScene: scene as! UIWindowScene)

并将它们添加到info.plist中:

<key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <true/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UILaunchStoryboardName</key>
                    <string>LaunchScreen</string>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>

这就是您需要做的。


我遇到了同样的问题,为$(PRODUCT_MODULE_NAME).解决问题加了前缀。
肖恩·豪威尔
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.