如何将 UIApplicationDelegateAdaptor 用作 ObservableObject?
How to use UIApplicationDelegateAdaptor as ObservableObject?
在我的 iOS 14 App
中,我可以通过以下方式注册遗产 AppDelegate
:
@main
struct MyApp: App {
#if os(iOS)
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
var body: some Scene {
...
}
}
#if os(iOS)
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
SomeSDK.configure(with: launchOptions)
return true
}
}
#endif
但是,我在 the documentation 中注意到您可以将 UIApplicationDelegateAdaptor
设为 ObservableObject
然后它会将其注入 EnvironmentObject
:
...delegate will be placed in the Environment and may be accessed by using the @EnvironmentObject
property wrapper in the view hierarchy.
我找不到任何示例说明它是如何工作的。使此工作成为 ObservableObject
的语法是什么?
这是一个使用演示
- 确认
AppDelegate
到ObservableObject
class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
@Published var value: String = ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
self.value = "test" // in any callback use your published property
return true
}
}
- 像您一样将您的
AppDelegate
注册为适配器
@main
struct Testing_SwiftUI2App: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
// ... other code
- 在您的某些视图中根据需要声明
@EnvironmentObject
struct ContentView: View {
@EnvironmentObject var appDelegate: AppDelegate // << inject
var body: some View {
Text("Value: \(appDelegate.value)") // << use
}
}
测试 Xcode 12 / iOS 14.
在我的 iOS 14 App
中,我可以通过以下方式注册遗产 AppDelegate
:
@main
struct MyApp: App {
#if os(iOS)
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#endif
var body: some Scene {
...
}
}
#if os(iOS)
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
SomeSDK.configure(with: launchOptions)
return true
}
}
#endif
但是,我在 the documentation 中注意到您可以将 UIApplicationDelegateAdaptor
设为 ObservableObject
然后它会将其注入 EnvironmentObject
:
...delegate will be placed in the Environment and may be accessed by using the
@EnvironmentObject
property wrapper in the view hierarchy.
我找不到任何示例说明它是如何工作的。使此工作成为 ObservableObject
的语法是什么?
这是一个使用演示
- 确认
AppDelegate
到ObservableObject
class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
@Published var value: String = ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
self.value = "test" // in any callback use your published property
return true
}
}
- 像您一样将您的
AppDelegate
注册为适配器
@main
struct Testing_SwiftUI2App: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
// ... other code
- 在您的某些视图中根据需要声明
@EnvironmentObject
struct ContentView: View {
@EnvironmentObject var appDelegate: AppDelegate // << inject
var body: some View {
Text("Value: \(appDelegate.value)") // << use
}
}
测试 Xcode 12 / iOS 14.