我在swift中有一个对象来管理MCSessions并连接到它找到的任何对象。但是,似乎总是存在一些问题,因此当它连接到会话时,它会断开连接。
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
browser.invitePeer(peerID, toSession: session, withContext: nil, timeout: 10)
print("Found someone looking for a session to join: " + peerID.displayName);
}
//Error function
func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError) {
print(error.localizedDescription)
}
func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
//Do something when we lost a connection to someone.
}
// Remote peer changed state.
func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState){
print("WE HAVE A PEER THATS CHANGING");
var str = "";
switch(state){
case .NotConnected: str = "Not Connected";
case .Connecting: str = "Connecting";
case .Connected: str="Connected";
}
print("Count: " + String(session.connectedPeers.count) + " State: " + str);
}
观察人们对此的其他问题,几乎总是由广告客户,浏览器和会话之间的MCPeerID不同引起的。但是,我明确地为整个类设置了一个常量peerID。
let devicePeerID = MCPeerID(displayName: UIDevice.currentDevice().name)
我在iPhone 5S和模拟器之间进行测试。