NSNotification.Name.NSExtensionHostWillEnterForeground does not work

384 Views Asked by At

I can use NSNotification in last version of Xcode 11.1. I can trying to update fields values with a my function refreshFields() after returning to the app from background. My code compiles successfully, but function applicationWillEnterForeground() never calling. Where is the mistake?

@objc func applicationWillEnterForeground(notification: NSNotification) {
    refreshFields()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear( animated)
    let app = UIApplication.shared
    NotificationCenter.default.addObserver(self, selector: #selector(self.applicationWillEnterForeground(notification:)), name: NSExtensionHostWillEnterForeground, object: app)
}
1

There are 1 best solutions below

0
On

I have found a solution. The mistake in name of Notification. It must be: UIApplication.willEnterForegroundNotification

@objc func applicationWillEnterForeground(notification: NSNotification) {
    refreshFields()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear( animated)
    let app = UIApplication.shared
    NotificationCenter.default.addObserver(self, selector: #selector(self.applicationWillEnterForeground(notification:)), name: UIApplication.willEnterForegroundNotification, object: app)
}