Contents

UIHostingSceneDelegate

Extends UIKit/UISceneDelegate to bridge SwiftUI scenes.

Declaration

protocol UIHostingSceneDelegate : UISceneDelegate

Overview

Declare any SwiftUI scenes you wish to activate from UIKit in the static rootScene property of your conforming class:

class HostingSceneDelegate: UIHostingSceneDelegate {
    static var rootScene: some Scene {
        WindowGroup(id: "swiftui-window") {
            ContentView()
        }
    }

    // Add UISceneDelegate lifecycle callbacks here
}

Use a class conforming to UIHostingSceneDelegate to activate a scene by its ID or presented value with UISceneSessionActivationRequest:

if let requestWithID = UISceneSessionActivationRequest(
    hostingDelegateClass: HostingSceneDelegate.self,
    id: "swiftui-window"
) {
    UIApplication.shared.activateSceneSession(for: requestWithID)
}

if let requestWithData = UISceneSessionActivationRequest(
    hostingDelegateClass: HostingSceneDelegate.self,
    value: FavoriteNumber(13)
) {
    UIApplication.shared.activateSceneSession(for: requestWithData)
}

When a SwiftUI scene declared in your rootScene property is activated, an instance of your conforming class will be created by SwiftUI and receive window scene lifecycle callbacks.

Your UIHostingSceneDelegate class can be used with a UISceneConfiguration in your app delegate’s application(_:configurationForConnecting:options:)method to activate a SwiftUI scene in response to an external event:

class AppDelegate: UIApplicationDelegate {

    func application(
        _ app: UIApplication,
        configurationForConnecting sceneSession: UISceneSession,
        options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
        let config = UISceneConfiguration(
            name: nil, sessionRole: sceneSession.role)
        config.delegateClass = HostingSceneDelegate.self
        return config
    }

}

Topics

Associated Types

Type Properties

See Also

Displaying SwiftUI views in UIKit