UPDATE在使用Apple支持凭单后,他们确认经常调用sendData并且数据太多会导致断开连接。
击球破发点并backgrounding时,当我有断开。由于应用商店不会发生断点,因此您需要在应用即将进入后台时开始后台任务来处理后台情况。然后在您的应用回到前台时结束此任务。在iOS 7上,这给了你大约3分钟的背景,比没有好。
另一个策略是在背景时间到期前使用[[UIApplication sharedApplication] backgroundTimeRemaining]
安排本地通知大概15秒钟,这样您可以在挂起应用程序之前将用户带回应用程序,并且必须关闭多对等框架。也许本地通知会警告他们,他们的会话将在10秒内过期...
如果后台任务到期并且应用程序仍在后台,则必须拆除与多点对等连接相关的所有内容,否则你会碰到崩溃。
- (void) createExpireNotification
{
[self killExpireNotification];
if (self.connectedPeerCount != 0) // if peers connected, setup kill switch
{
NSTimeInterval gracePeriod = 20.0f;
// create notification that will get the user back into the app when the background process time is about to expire
NSTimeInterval msgTime = UIApplication.sharedApplication.backgroundTimeRemaining - gracePeriod;
UILocalNotification* n = [[UILocalNotification alloc] init];
self.expireNotification = n;
self.expireNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:msgTime];
self.expireNotification.alertBody = TR(@"Text_MultiPeerIsAboutToExpire");
self.expireNotification.soundName = UILocalNotificationDefaultSoundName;
self.expireNotification.applicationIconBadgeNumber = 1;
[UIApplication.sharedApplication scheduleLocalNotification:self.expireNotification];
}
}
- (void) killExpireNotification
{
if (self.expireNotification != nil)
{
[UIApplication.sharedApplication cancelLocalNotification:self.expireNotification];
self.expireNotification = nil;
}
}
- (void) applicationWillEnterBackground
{
self.taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^
{
[self shutdownMultiPeerStuff];
[[UIApplication sharedApplication] endBackgroundTask:self.taskId];
self.taskId = UIBackgroundTaskInvalid;
}];
[self createExpireNotification];
}
- (void) applicationWillEnterForeground
{
[self killExpireNotification];
if (self.taskId != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.taskId];
self.taskId = UIBackgroundTaskInvalid;
}
}
- (void) applicationWillTerminate
{
[self killExpireNotification];
[self stop]; // shutdown multi-peer
}
您也想这个处理程序在你的MCSession代表由于苹果的bug:
- (void) session:(MCSession*)session didReceiveCertificate:(NSArray*)certificate fromPeer:(MCPeerID*)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler
{
if (certificateHandler != nil) { certificateHandler(YES); }
}
我有同样的问题,但我有一些数据发送后断开连接。你有解决这个问题吗? – Moonkid
我注意到的一件事是在调试器中暂停MCSession。我最终编写了一个机制来重新建立会话,如果它被丢弃。 – tillerstarr
我有同样的问题。我注意到,如果一个设备是背景灯,并且消息被发送给它,则会发生断开连接。 – jjxtra