2

I am having a weird issue with a SpriteKit game. The app crashes when a user opens Notification center or Control Center. I want the worldNode layer to be paused and the menuNode layer to be presented when the applicationWillResignActive is called, which is working fine when the home button is pressed. I've tried pausing my game from within the AppDelegate functions applicationWillResignActive and I've tried pausing when applicationDidBecomeActive is called if the game has started. I've tried using the NotificationCenter approach from within the ViewController both work when the Home Button/Home Gesture is used. How Should I handle this? Or is this just a bug in iOS 11?

I have this in the viewDidLoad method of my ViewController

NotificationCenter.default.addObserver(self, selector:  #selector(appWillResignActive), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

Then each one of those selectors It crashes on both if trying to present the menu when the appDidBecomeActive and appWillResignActive

@objc func appDidBecomeActive() {
    print("appDidBecomeActive")

    if GameScene.sharedInstance != nil {
        print("presentingMenu")
        GameScene.sharedInstance.presentMenuNode()
    }
}

@objc func appWillResignActive() {
    print("appWillResignActive")

    if GameScene.sharedInstance != nil {
        print("presentingMenu")
        GameScene.sharedInstance.presentMenuNode()
    }

}

I feel like I may be trying to approach this the wrong way, but what I don't understand is why does it work when the Home button/Home gesture is fired?

Edit: After more testing I found that everything works as expected when running on iOS 10 devices. However when I run the DemoBots app that apple provides from their sample code it doesn't crash on iOS 11 and basically does what I want to do, so there has got to be a better way to handle the transitions of the game state, any input is appreciated.

  • Not sure I am following you here, but something in presentMenuNode is causing the crash. unfortunately there are way to many possibilities that could cause it for me to just guess. Provide a MVCE and we will be able to assist you better. https://stackoverflow.com/help/mcve – Knight0fDragon Jan 04 '18 at 14:34
  • I kinda got it worked out but I wasn't answering in case someone came along to provide a nice solution, Like I stated though everything works fine on iOS 10 devices but not on iOS 11. The crash is coming from my `menuNode` layer when I call `addChild(menuNode)` I found that if I call `menuNode.removeAllChildren()` in the `appWillResignActive()` method and then call `presentMenuNode()` in `appDidBecomeActive()` everything works, but still feels like a bug since everything is working fine without this when using Home Button/Gestures on iOS 11 and in all cases on iOS 10 – CarpenterBlood Jan 04 '18 at 14:44
  • You have something going on, do not just accept it works because iOS 11 works, it may end up breaking in iOS 12 again when they change things. Explain what the actual error is instead of just calling it a “crash” and people can help more. – Knight0fDragon Jan 04 '18 at 14:48

0 Answers0