简体   繁体   English

如何正确处理GKSession的中断?

[英]How to properly handle interruptions of GKSession?

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. 当用户听音乐并退出应用程序(进入背景)以寻找其他地方时,我拆掉了GKSession。 It's P2P mode. 这是P2P模式。 When user returns to app, I reconnect GKSession. 当用户返回应用程序时,我重新连接GKSession。

Is this the right thing? 这是对的吗? And what to do if a phone call arrives or other interruption occurs? 如果电话到达或其他中断发生怎么办? Also kill GKSession and recreate? 杀死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];

You're doing it right. 你说得对。 All threads are suspended when your app is in background mode, including GKSession 's. 当您的应用处于后台模式时,所有线程都会被暂停,包括GKSession

GKSession still works when your app is in 'Inactive' mode (eg when it's interrupted by a phone call, etc.). 当您的应用处于“非活动”模式时(例如,当它被电话中断等)时, GKSession仍然有效。 Inactive means that the app is still running in the foreground but it's not receiving events. 非活动意味着应用程序仍在前台运行,但它没有接收事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM