因此,我的理想情况是我希望我的 MCNearbyServiceAdvertiser
和 MCNearbyServiceBrowser
在后台工作。但我知道这些需要在应用程序退出事件时被杀死,并在我回到前台时恢复工作。
但让我烦恼的是,当我将两个对等点连接到一个 session ,并开始在他们之间聊天时,它突然断开连接。意思是..我的 MCSession 对象在进入后台时断开连接。我还被告知 certificateHandler(YES);
的 Apple 错误,我现在明确地调用它。
我想准确地设置 FIRECHAT
的操作方式。任何人都可以给我一些指示,说明为什么它总是失败,或者你们如何设法保持它活跃吗?
谢谢,
最佳答案
为了让您的同伴保持连接,您需要在应用进入后台时启动后台任务,否则 iOS 将断开网络连接并暂停应用。
- (void)applicationDidEnterBackground:(UIApplication *)application {
//Start a background task to keep the app running in the background
self.bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
//If your background task takes too long, this block of code will execute
[self cleanUp];
self.bgTask = UIBackgroundTaskInvalid;
}];
//Do the work you need to do
dispatch_async(dispatch_get_main_queue(), ^{
//Finish up the transfer of data between peers
[self cleanUp];
//End the background task so that iOS doesn't kill the app
[application endBackgroundTask:_bgTask];
});
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[application endBackgroundTask:_bgTask];
}
- (void)cleanUp {
//Clean up the Multipeer session
}
请注意,这仅适用于应用程序在前台时建立的现有连接。进入后台还是要停止浏览和广告。
关于ios - 多点连接在后台断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24746356/