I have a simple ObservableObject
class which I initialize in the App.swift file and pass to the first view:
final class Counter: ObservableObject {}
@main
struct MyCoolApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self)
private var appDelegate
private let counter = Counter()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(counter)
}
}
}
That works great, but now I need to get at it from my notification delegate. Essentially I have this:
class AppDelegate: NSObject, UIApplicationDelegate {
let notificationDelegate: NotificationDelegate
func registerForPushNotifications(application: UIApplication) {
// irrelevant code removed.
let center = UNUserNotificationCenter.current()
center.delegate = self?.notificationDelegate
}
}
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
}
In my NotificationDelegate
class, how do I get access to the Counter
class that I instantiated?
Here is possible way - to use shared instance (because in your scenario anyway it is used only one instance)