I have declared notification name in a swift file which has target membership of both main app and today widget:
let SpecialKey = Notification.Name("howLongNotif")
Then in my main app view controller, I am posting notification when location is updated (background mode for location updates is on):
NotificationCenter.default.post(name: SpecialKey, object: nil, userInfo: nil)
In my today widget viewDidLoad, I am observing it like this:
NotificationCenter.default.addObserver(self, selector: #selector(TodayViewController.dataReceived), name: SpecialKey, object: nil)
and have:
func dataReceived(_notification: NSNotification) {
print("data received")
}
But dataReceived
function is never invoked.
I tested by moving the observer and dataReceived function to main app and it works fine there.
Your main app and today is extension are run as separate processes on the phone.
NSNotificationCenter
only works within a single process.To pass information between your extension and main app, you can use
NSUserDefaults
or a file in the shared container.