这里有一个类似的问题:Multipeer Connectivity Framework - Lost Peer stays in Session
但遗憾的是答案没有帮助,或者我误解了它们。
我有我的多重连接应用程序,并且还测试了Apples自己的MultipeerGroupChat应用程序。
当对等方断开连接时,有时它们会留在会话中。当对等体尝试重新连接时,这会导致连接失败。
我在applicationWillTerminate中尝试过[session disconnect],但它仍然会发生。
如何让MCBrowserViewController更新自己与谁连接以及谁不是?
-(id)init{
self = [super init];
if (self) {
_peerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
-(void)setupMCBrowser{
_browser = [[MCBrowserViewController alloc] initWithServiceType:@"session" session:_session];
_browser.maximumNumberOfPeers = 8;
}
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
NSDictionary *dict = @{@"peerID": peerID,
@"state" : [NSNumber numberWithInt:state]
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeStateNotification"
object:nil
userInfo:dict];
switch (state)
{
case MCSessionStateConnecting:
{
_statusID = @"Connecting";
break;
}
case MCSessionStateConnected:
{
_statusID = @"Connected";
if (_firstPeer == NULL){
_firstPeer = peerID.displayName;
}
else if (_secondPeer == NULL){
_secondPeer = peerID.displayName;
}
else if (_thirdPeer == NULL){
_thirdPeer = peerID.displayName;
}
break;
}
case MCSessionStateNotConnected:
{
if ([peerID isEqual:_firstPeer]){
_firstPeer = NULL;
}
else if ([peerID isEqual:_secondPeer]){
_secondPeer = NULL;
}
else if ([peerID isEqual:_thirdPeer]){
_thirdPeer = NULL;
}
_statusID = @"Connection Lost";
break;
}
}
}