I need a function that will be called when the button is pressed home, or simply when the application is closed

  • If possible, publish the solution found in response to your question . I am sure it will help many of your colleagues in the future. - Nicolas Chabanovsky

1 answer 1

When you press the home button, the application is minimized, and the applicationDidEnterBackground:(UIApplication *)application method in the AppDelegate works accordingly. You only need to implement this method in AppDelegate.m (.swift)

  • it is not clear, it is possible in more detail how to push function from appdelegate in viewcontroller? - Dani
  • I figured it out myself, you need to write this code in the AppDelegate func applicationDidEnterBackground(application: UIApplication) { print("bye") NSNotificationCenter.defaultCenter().postNotificationName("out", object: self) } And this is put into the desired ViewController override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(home), name: "out", object: nil) } Everything works fine for me. And I don’t want to switch to a new line, neither 2 spaces, nor <br/> help me - Dani