black screen when I run my iOS application

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.


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

Tags:

Ios