多重连接在背景上断开连接

时间:2014-07-14 21:34:10

标签: ios cocoa-touch multipeer-connectivity

所以,我理想的情况是我希望我的MCNearbyServiceAdvertiserMCNearbyServiceBrowser在后​​台工作。但我明白,当应用程序重新启动时,这些需要被杀死,并且当我回到前台时恢复工作。

但令我烦恼的是,当我将两个对等方连接到一个会话,并开始在它们之间聊天时,它突然断开连接。含义..我的MCSession对象在进入后台时断开连接。我还被告知了certificateHandler(YES);的Apple bug,我现在明确地称之为。

我想准确地说明FIRECHAT的确切方式。任何人都可以给我一些指示,说明为什么它会一直失败,或者你们如何设法让它保持活跃状态​​?

谢谢,

1 个答案:

答案 0 :(得分:3)

为了让您的同伴保持联系,您需要在应用进入后台时启动后台任务,否则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
}

请注意,这仅适用于应用在前台时创建的现有连接。输入背景时,您仍然需要停止浏览和广告。

相关问题