2

I have a silent remote notification coming in on a SwiftUI app. It is not picked up by the UNUserNotificationCenter, but rather, by the old AppDelegate didReceiveNotification func. What is the solution for notifying ContentView() that a change has occurred? An ObservableObject in AppDelegate?

1 Answer 1

2

You can declare a store which conforms ObservableObject in the AppDelegate and set it as an environment object to the ContentView.

// AppDelegate.swift
// in the class
let store = Store()

// in the applicationDidFinishLaunching(_:) method
window.contentView = NSHostingView(rootView: contentView.environmentObject(store))
// ContentView.swift
// in the struct
@EnvironmentObject var store: Store

The environment object is a reference so you can pass the value into it. Same to using ObservableObject. When you finish updating just call objectWillChange.send() or mark the property to @Published. And ContentView will be updated after notified.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.