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