I have a situation in which i have to intialize an object everytime when it comes from background to foreground and that should be using the NSNotificationCenter not with ap
Swift 5
Subscribe to Notification -
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(
self,
selector: #selector(applicationWillEnterForeground(_:)),
name: UIApplication.willEnterForegroundNotification,
object: nil)
}
Remove subscription -
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
Function to be called -
@objc func applicationWillEnterForeground(_ notification: NSNotification) {
self.volumeSlider.value = AVAudioSession.sharedInstance().outputVolume
}