I'm experimenting with Multipeer Connectivity Framework in iOS. I want to make a simple app that advertises the device and also browses for other devices. Here is my code:
class ViewController: UIViewController {
static let serviceType = "mult-demo"
let localPeerID = MCPeerID(displayName: UIDevice.currentDevice().name)
func startAdvertising() {
let advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: nil, serviceType: ViewController.serviceType)
advertiser.delegate = self
advertiser.startAdvertisingPeer()
print("advertising")
}
func startBrowsing() {
let browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: ViewController.serviceType)
browser.delegate = self
browser.startBrowsingForPeers()
print("browsing")
}
override func viewDidLoad() {
super.viewDidLoad()
startAdvertising()
startBrowsing()
}
}
extension ViewController : MCNearbyServiceAdvertiserDelegate, MCNearbyServiceBrowserDelegate {
func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession) -> Void) {
print("received invitation")
}
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
print("found peer \(peerID)")
}
func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
}
}
However, when I run it on two iPads (iOS 9) connected to the same wifi network they don't discover each other. What am I doing wrong?
browserandadvertiserin properties rather than local variables as they will be released once the functions exit - Paulw11