Initializer

init(_:)

Creates a UIKit app delegate adaptor using a delegate that’s an observable object.

Declaration 宣言

init(_ delegateType: DelegateType.Type = DelegateType.self)
Available when DelegateType inherits NSObject, DelegateType conforms to ObservableObject, and DelegateType conforms to UIApplicationDelegate.

Parameters パラメータ

delegateType

The type of application delegate that you define in your app, which conforms to the UIApplicationDelegate and ObservableObject protocols.

Discussion 議論

Call this initializer indirectly by creating a property with the UIApplicationDelegateAdaptor property wrapper from inside your App declaration:


@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor private var appDelegate: MyAppDelegate


    var body: some Scene { ... }
}

SwiftUI initializes the delegate and manages its lifetime, calling it as needed to handle application delegate callbacks.

SwiftUI invokes this method when your app delegate conforms to the ObservableObject protocol. In this case, SwiftUI automatically places the delegate in the Environment. You can access such a delegate from any scene or view in your app using the EnvironmentObject property wrapper:


@EnvironmentObject private var appDelegate: MyAppDelegate

If your delegate isn’t an observable object, SwiftUI invokes the init(_:) initializer rather than this one, and doesn’t put the delegate instance in the environment.

See Also 参照

Creating a Delegate Adaptor