3

My online radio app is playing background music. When the user listens to music and exits the app (enters background) to look elsewhere, I tear down the GKSession. It's P2P mode. When user returns to app, I reconnect GKSession.

Is this the right thing? And what to do if a phone call arrives or other interruption occurs? Also kill GKSession and recreate?

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

    // Register for notifications when the application leaves the background state
    // on its way to becoming the active application.
    [defaultCenter addObserver:self 
                      selector:@selector(setupSession) 
                          name:UIApplicationWillEnterForegroundNotification
                        object:nil];

    // Register for notifications when when the application enters the background.
    [defaultCenter addObserver:self 
                      selector:@selector(teardownSession) 
                          name:UIApplicationDidEnterBackgroundNotification 
                        object:nil];
openfrog
  • 40,201
  • 65
  • 225
  • 373

1 Answers1

0

You're doing it right. All threads are suspended when your app is in background mode, including GKSession's.

GKSession still works when your app is in 'Inactive' mode (e.g. when it's interrupted by a phone call, etc.). Inactive means that the app is still running in the foreground but it’s not receiving events.

Marco
  • 6,692
  • 2
  • 27
  • 38