There is a similar question to this here:Multipeer Connectivity Framework - Lost Peer stays in Session
But the answers have unfortunately not helped, or I have misunderstood them.
I have my multipeer connectivity app, and have also tested Apples own MultipeerGroupChat app.
When a peer disconnects, sometimes they stay in the session. This results in failed connections when the peer tries to reconnect.
I have tried [session disconnect] in applicationWillTerminate, but it still occurs.
How can I have the MCBrowserViewController update itself with who is connected and who isn't?
-(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;
}
}
}