How can I use applicationWillTerminate for a specific SKScene?

I have SKScene

one that displays the players who have joined the current room. If any of these players leave the room (by clicking the "Leave" button), the list of players will be updated.

But if I close the app from one of the players, that particular player remains in the room. I want to call a function leaveRoom

from applicationWillTerminate

so that all data works fine. Is it possible? How can I solve this problem?

+3


source to share


1 answer


You can force the 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 an observer to:

override func willMove(from view: SKView) {
    NotificationCenter.default.removeObserver(self)
}

      

+3


source







All Articles