6

SpriteKit is supposed to clean up and pause all timers when you press the home button.

However, we have found that if you single tap the home button while displaying an active SKView, the app crashes. This occurs even if that view is already paused by the user.

Strangely, if you double tap the home button and move to the multitasking view, everything works fine.

Follow Up Note: The simulator works perfectly in both situations, no crash

Is anyone else seeing this issue with SpriteKit?

Have you found the cause / solution?

MobileVet
  • 2,958
  • 2
  • 26
  • 42

4 Answers4

10

I was having the same problem and I solved it manually pausing the root SKView in the ViewController before the app moving to the background:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view (already setup as SKView via storyboard)
    SKView * skView = (SKView *)self.view; 

    // Create and configure the scene.
    SKScene *scene = [Menu sceneWithSize:CGSizeMake(skView.bounds.size.height, skView.bounds.size.width)];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterBackground)
    name:UIApplicationWillResignActiveNotification
    object:NULL];
}

- (void)appWillEnterBackground
{
    SKView *skView = (SKView *)self.view;
    skView.paused = YES;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterForeground)
    name:UIApplicationWillEnterForegroundNotification
    object:NULL];
}

- (void)appWillEnterForeground
{
    SKView * skView = (SKView *)self.view;
    skView.paused = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(appWillEnterBackground)
    name:UIApplicationWillResignActiveNotification
    object:NULL];
}
tanz
  • 2,557
  • 20
  • 31
  • This is definitely the more common issue people will have... so I accepted it. – MobileVet Jun 04 '14 at 19:49
  • 1
    Tanzolone's answer is great, however, if you pull down the Notification Center the app will pause, but it won't unpause when you slide the Notification Center back up. I just had this problem and figured others might so this is what I did: Find "-(void)AppWillEnterBackground" change "name:UIApplicationWillEnterBackground" to "name:UIApplicationDidBecomeActiveNotification" – Joseph Bergman Jul 20 '14 at 19:40
0
  1. Run Xcode's analyzer (Project > Analyze) to determine if you have memory management issues in your code.

  2. Run this scenario in the allocations instrument of the Instruments app (Project > Profile), which can report any detected misuse of memory.

bneely
  • 9,083
  • 4
  • 38
  • 46
0

We ran into some similar inconsistent behavior with UICollectionView. In that case, instantiating the object via a Storyboard (versus programmatically) resolved the issue.

On a hunch, I tried that this morning and Viola, success!

So, it appears that Storyboards are doing some magic with SKView on creation that is unknown to us and allows them to be properly terminated on Home Button press. Frustrating, but at least it works now.

MobileVet
  • 2,958
  • 2
  • 26
  • 42
  • Additionally, setting the FPS for an SKView only works if you instantiate the view in a storyboard – MobileVet Nov 07 '13 at 14:21
  • More information, this may have been caused by our call to 'init' instead of 'initWithCoder' or 'initWithFrame', those appear to work correctly as well. – MobileVet Nov 11 '13 at 18:59
0

The problem can be caused by audio, if so you can check the answer here https://stackoverflow.com/a/19283721/1278463

I solved this in a game which is not using audio. Th solution is to pause SKView when entering background:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    SKView *view = (SKView *)self.window.rootViewController.view;
    if (view) {
        view.paused = YES;
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    SKView *view = (SKView *)self.window.rootViewController.view;
    if (view) {
        view.paused = NO;
    }
}
Community
  • 1
  • 1
Borislav Gizdov
  • 1,323
  • 12
  • 22