See also: UIApplicationDelegate
documentation
// AppDelegate.swift
@main
final class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let sceneConfiguration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
sceneConfiguration.delegateClass = SceneDelegate.self
return sceneConfiguration
}
}
See also: UIWindowSceneDelegate
documentation
// SceneDelegate.swift
final class SceneDelegate: NSObject, UIWindowSceneDelegate {
var window: UIWindow?
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
guard let windowScene = scene as? UIWindowScene else {
return
}
let window = UIWindow(windowScene: windowScene)
// TODO: Do something with `window`
let contentView = ContentView() // or whatever your root SwiftUI view is called
window.rootViewController = UIHostingController(rootView: contentView)
window.makeKeyAndVisible()
self.window = window
}
}
In a newly created SwiftUI project:
- Click your project name in the left file navigator
- Click the app target in the following screen
- Click 'Info'
- Expand 'Application Scene Manifest'
- Add a row inside 'Application Scene Manifest' – Xcode will generate a
<Your-Target-Name>-Info.plist
file in the left file navigator - Right click on the generated plist file, and choose Open As → Source Code
- Add the scene configuration below inside the
<dict>
forUIApplicationSceneManifest
In a project with an existing Info.plist file:
- Right click on the plist file in the left file navigator, and choose Open As → Source Code
- Add the scene configuration below inside the
<dict>
forUIApplicationSceneManifest
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
</dict>
@UIApplicationMain
seems to be deprecated, now you can use@main