Jorgen
Jorgen

Reputation: 475

Multipeer lost and found

I have an app I'm working on where I'm using the Multipeer Framework to send data between some iPads. I'm setting up the session using the MCBrowserViewController and ConnectionViewController, all as per Apple's examples.

This works pretty good and I can send my data across, both ways.

However, if one of the devices loose the connection I don;'t seem to be able to reconnect again when it's found.

Using MCNearbyServices like Advertiser and Browser doesn't seem to do anything. LostPeer and FoundPeer does never get called. I have both delegates (Advertiser and Browser) in my MCManager class. Surely I should be able to reconnect automatically when they are back in range or the app is running again?

I've tried this after @bradenm suggestion...

    -(void)reconnect
{
    _myPeerID = [[MCPeerID alloc]initWithDisplayName:[UIDevice currentDevice].name];

    _nearbyAdvertiser = [[MCNearbyServiceAdvertiser alloc]
                         initWithPeer:_myPeerID
                         discoveryInfo:nil
                         serviceType:kServiceType];

    _nearbyAdvertiser.delegate = self;
    _nearbyBrowser.delegate = self;

    [self setAdvertising:YES];
    [self setBrowsing:YES];

}

But I don't get any of the other methods called (foundPeer and so on)

Upvotes: 0

Views: 434

Answers (1)

bradenm
bradenm

Reputation: 2170

I've found that re-using MCPeerID objects after an intentional or unintentional disconnect from an MCSession is very buggy and often impossible (invitations always fail). What's worked well for my app is to simply re-create a new MCPeerID after disconnecting (and re-create a new MCNearbyServiceAdvertiser with the new MCPeerID). You should then be able to connect again.

Upvotes: 1

Related Questions