我无法在我的视图控制器文件中完全实现MCNearbyServiceBroswerDelegate和MCNearbyServiceAdvertisingDelegate。
我的代码中填充了MCSession值,我得到的唯一错误是:
“类型'MCPManager'不符合协议'MCNearbyServiceBroswerDelegate'”
我查看了Apple的文档,并完全实现了协议的所有委托方法。我很茫然。
import MultipeerConnectivity
protocol MPCManagerDelegate {
func foundPeer()
func lostPeer()
func invitationWasReceived(fromPeer: String)
func connectedWithPeer(peerID: MCPeerID)
}
class MPCManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
var delegate: MPCManagerDelegate?
var session: MCSession!
var peer: MCPeerID!
var browser: MCNearbyServiceBrowser!
var advertiser: MCNearbyServiceAdvertiser!
var foundPeers = [MCPeerID]()
//var invitationHandler: ((Bool, MCSession!)->Void)!
var invitationHandler: (Bool, MCSession) -> Void = { success, session in }
override init() {
peer = MCPeerID(displayName: UIDevice.currentDevice().name)
session = MCSession(peer: peer)
browser = MCNearbyServiceBrowser(peer: peer, serviceType: "appcoda-mpc")
browser.delegate = self
advertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: nil, serviceType: "appcoda-mpc")
advertiser.delegate = self
super.init()
session.delegate = self
}
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
foundPeers.append(peerID)
delegate?.foundPeer()
}
func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
for (index, aPeer) in foundPeers.enumerate() {
if aPeer == peerID {
foundPeers.removeAtIndex(index)
break
}
}
delegate?.lostPeer()
}
func browser(browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: NSError) {
print(error.localizedDescription)
}
func sendData(dictionaryWithData dictionary: Dictionary<String, String>, toPeer targetPeer: MCPeerID) -> Bool {
let dataToSend = NSKeyedArchiver.archivedDataWithRootObject(dictionary)
let peersArray = NSArray(object: targetPeer)
//var error: NSError?
if self.session.connectedPeers.count > 0 {
//if let msg = self.messageField.text!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
do {
try self.session.sendData(dataToSend, toPeers: peersArray as! [MCPeerID], withMode: MCSessionSendDataMode.Reliable)
} catch let error as NSError {
print(error.localizedDescription)
}
}
return true
}
func advertiser(advertiser: MCNearbyServiceAdvertiser!, didReceiveInvitationFromPeer peerID: MCPeerID!, withContext context: NSData!, invitationHandler: ((Bool, MCSession!) -> Void)!) {
self.invitationHandler = invitationHandler
delegate?.invitationWasReceived(peerID.displayName)
}
func session(session: MCSession, peer peerID: MCPeerID
, didChangeState state: MCSessionState) {
switch state{
case MCSessionState.Connected:
print("Connected to session: \(session)")
delegate?.connectedWithPeer(peerID)
case MCSessionState.Connecting:
print("Connecting to session: \(session)")
default:
print("Did not connect to session: \(session)")
}
}
func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError) {
print(error.localizedDescription)
}
func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
let dictionary: [String: AnyObject] = ["data": data, "fromPeer": peerID]
NSNotificationCenter.defaultCenter().postNotificationName("receivedMPCDataNotification", object: dictionary)
}
func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) { }
func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) { }
func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) { }
}