3

I have an SKScene that displays the players that joined the current room. If any of those players leaves the room (by clicking on the Leave button) their players list will be updated.

But if I close the app from one of the players, that specific player remains in the room. I want to call my leaveRoom function from the applicationWillTerminate so all the data will work fine. Is it possible? How can I resolve this issue?

Sorin
  • 43
  • 1
  • 5
  • As an additional info to @Alessandro's answer which solves the issue, read a discussion section from the [docs](https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623111-applicationwillterminate) to find out when this method is actually called and when not. – Whirlwind Mar 27 '17 at 12:45

1 Answers1

3

You can make an observer to intercept it:

override func didMove(to view: SKView) {        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(GameScene.applicationWillTerminate(notification:)),
            name: NSNotification.Name.UIApplicationWillTerminate,
            object: nil)
}
func applicationWillTerminate(notification: NSNotification) {
   // put your code here
}

You can remove the observer to :

override func willMove(from view: SKView) {
    NotificationCenter.default.removeObserver(self)
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133