While playing with Sprite Kit recently I ran into an odd crasher when backgrounding the app that threw me for a loop.
The stack trace was showing gpus_ReturnNotPermittedKillClient:
as the offending method. Of course this isn't in my code anywhere.
After some trial and error with the code I tracked the crash down to sounds being played. Even if a sound had been played and was now finished it would still crash. A little further investigation led me to find out that this crash was related to AVAudioSession
when being backgrounded. Sprite Kit uses AVFoundation
under the hood to make it easy for you to play your sounds and music within your game.
But AVAudioSession
apparently cannot be active while in the background (it causes the crash if it is)... The fix was simple: deactivate the audio session when the app enters the background and reactivate it when it returns.
//
// SSAppDelegate.m
// Space Ship
//
// Created by James Addyman on 18/12/2013.
// Copyright (c) 2013 James Addyman. All rights reserved.
//
#import "SSAppDelegate.h"
@import AVFoundation;
@implementation SSAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[[AVAudioSession sharedInstance] setActive:NO error:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[AVAudioSession sharedInstance] setActive:NO error:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end
Since Sprite Kit sets up the audio session for you within the framework, I would expect this to be handled by the framework and not crash your app in this way. I will be filing a bug report with Apple and I suggest everyone else does the same so that they fix it as soon as possible. I'll update this post when I get a radar number so it can be used to dupe the issue.