2012-11-21 106 views
3

我的在线广播应用程序正在播放背景音乐。当用户收听音乐并退出应用程序(进入后台)以查看其他地方时,我会拆除GKSession。它是P2P模式。当用户返回到应用程序时,我重新连接GKSession。如何正确处理GKSession的中断?

这是正确的吗?如果接到电话或发生其他中断,该怎么办?还杀GKSession并重新创建?

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]; 
+0

这就是我通常所做的,但我不确定这是否是最佳做法。 – msgambel

回答

0

你做得对。当您的应用处于后台模式时,所有线程都会暂停,包括GKSession

GKSession当您的应用程序处于“未激活”模式时(例如,当它被电话等打扰时)仍然有效。非活动意味着应用程序仍在前台运行,但它不接收事件。