My app is playing a video and I want to trigger an action when the video ends. The screen (A) is embedded in a navigation controller, and if I trigger a push (to B) and then come back (to A), the action (in A) still takes place based on the observer. There is also an option in my screen that triggers a modal (to C) which then gets dismissed to go back (to A). When I come back from the modal (C), however, the observer (in A) is gone.
Here's my code for screen A's view controller:
ScreenAViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do more stuff
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.avPlayer.currentItem, queue: .main) { _ in
// Do stuff
}
}
}
Here's the code that trigger the modal to screen C:
@IBAction func triggerModal(_ sender: UIButton) {
avPlayer.pause()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let screenCViewController = storyboard.instantiateViewController(withIdentifier: "ScreenC") as! ScreenCViewController
present(screenCViewController, animated: true)
}
And finally here's the line that dismisses screen C:
dismiss(animated: true, completion: nil)