So 2 weeks ago I submitted a sprite kit app to the app store and it all was fine. I was having problems before i submitted the app where it would crash because of AvAudioSession, however i was able to fix that problem through this Sprite Kit & playing sound leads to app termination. This basically sets AVAudioSession to inactive when going into background and then active again when coming into foreground. I recently update my phone it iOS 7.1 and this fix doesn't seem to work in the new 7.1 and my app is again crashing whenever it enters the background. I have taken all the audio out of my app and it seems to work fine so it is the same problem as i had before just now the solution doesn't work!I really need to fix this problem as i have an update ready to submit.. Cheers Sam
I did it!
I just paused the SKView in- (void)applicationWillResignActive:(UIApplication *)application
and included the AVAudioSession set to inactive.
AppDelegate.h
#import <SpriteKit/SpriteKit.h>
AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application
{
// prevent audio crash
[[AVAudioSession sharedInstance] setActive:NO error:nil];
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[AVAudioSession sharedInstance] setActive:NO error:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO;
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}
adding to @ObjectivesCsam answer:
when rootViewControllers view is not SKView u can use
- (SKView *)getGameView {
NSArray *viewControllers = self.window.rootViewController.childViewControllers;
for (UIViewController *vc in viewControllers) {
if ([vc.view isKindOfClass:[SKView class]]) {
SKView *view = (SKView *)vc.view;
return view;
}
}
return nil;
}
来源:https://stackoverflow.com/questions/22407111/ios-7-1-sprite-kit-avaudiosession-crash-when-enter-background