Search code examples
iosswiftmacoscocoamultipeer-connectivity

Multipeer Connectivity automatic silent connection


I am trying to create some iOS app which would be manageable by OS X app via MultiPeer Connectivity framework. I am a newbie with this framework, but everything works well so far and I am wondering if it is possible to automatically connect to peer if the connection is lost.

I went through some ideas here on stackoverflow and other websites, which recommend to use MCNearbyServiceBrowserDelegate with its function startBrowsingForPeers() to start browsing for peers when I detect that peer has been lost (this detectin works also well for me).

In next step, this function should call a function: browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) from MCNearbyServiceBrowserDelegate, but in my case, it looks the function browser found peer is never called. Any ideas?

Thanks


Solution

  • @dgatwood Yeap exactly! Thank you. I just found it. :)

    For others in case that you get to same situation - you have to delegate it correctly, for example in method which reacts on changing state of peer:

    appDelegate.mpcHandler.autoBrowser = MCNearbyServiceBrowser.init(peer: peerID, serviceType: "network")
    appDelegate.mpcHandler.autoBrowser!.startBrowsingForPeers()
    appDelegate.mpcHandler.autoBrowser!.delegate = self
    

    Of course with appropriate definition of autoBrowser:MCNearbyServiceBrowser? in mpcHandler class or other class which is working with and inherits MCNearbyServiceBrowserDelegate protocol ...