black screen when I run my iOS application

Viewed 9083

I'm trying to do a new iOS app in Xcode. I made a main storyboard and I added a label on my ViewController. When I run my application, first second it show the label and then become the screen black without any errors.

I'm working on Xcode 11 (Swift 5) and this message appears on output:

[SceneConfiguration] Info.plist configuration "Default Configuration" for UIWindowSceneSessionRoleApplication contained UISceneDelegateClassName key, but could not load class with name "gina.SceneDelegate"

I don't know where my mistake is.

black screen when running

2 Answers

iOS 13 & above

Only if target is 13 or greater.

SceneDelegate is not supported before iOS 13. If you want to use SceneDelegate and also want to support iOS prior to iOS 13 then you a have to add some changes to your project.

  1. Add availability attribute to the whole class in SceneDelegate.swift file.
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
   ...
}
  1. AppDelegate.swift file has two new SceneDelegate method. Add availability attribute to them as well.
@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. Lastly, add UIWindow object in AppDelegate.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 and earlier

AppDelegate needs a UIWindow property. iOS 13 uses SceneDelegate in new projects. Specify the UIWindow object and remove the SceneDelegate.swift file.

If you have removed the SceneDelegate from project, then you must remove the Application Scene Manifest dictionary from Info.plist.

Info.plist

You need to initialize the window like this:

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

and add these in 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>

That's all you need to do.

Related